Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8111609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:11:13+00:00 2026-06-06T02:11:13+00:00

I have a JSON-like data structure (which I don’t want to change) that is

  • 0

I have a JSON-like data structure (which I don’t want to change) that is currently generated by an .aspx file spitting out javascript. It looks like .NET ate jQuery for lunch, and then vomited…

I’d like to rewrite the entire thing to an MVC controller action that returns a JsonResult, basically by building up an anonymous object and pass it to return Json(data).

However, I can’t figure out how to construct the C# object when some of the properties on the JSON object I want to build are actually JavaScript functions. How do I do that?

Example:

I want to create the following JSON-like object:

{
    id: 55, 
    name: 'john smith', 
    age: 32,
    dostuff: aPredefinedFunctionHandle,
    isOlderThan: function(other) { return age > other.age } 
}

You see that I want to be able to specify both function handles to JavaScript functions that I have defined elsewhere (in .js files, usually) and that I want to define new inline functions.

I know how to build part of that object in C#:

var data = new { id = 55, name = "john smith", age = 32 };
return Json(data);

Is there any good way to do also the rest of it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T02:11:14+00:00Added an answer on June 6, 2026 at 2:11 am

    There is no built-in type in .NET that maps to javascript function. So you may have to create a custom type that represents a function and you have to do the serialization yourself.

    Something like this..

    public class JsFunction
    {
       public string FunctionString{get; set;}
    }
    
    new
    {
        id = 55, 
        name = 'john smith', 
        age = 32,
        dostuff = new JsFunction{ FunctionString = "aPredefinedFunctionHandle" },
        isOlderThan = new JsFunction{ FunctionString = "function(other) { return age > 
                        other.age" } 
    }
    

    On serialization you may have to check the type of the value and write the FunctionString directly into the response without double-quotes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting JSON string from website. I have data which looks like this
I have a complex Clojure data structure that I would like to serialize -
I'm new to the GSON library, I have a json data structure that looks
I have a JSON tree structure that is generated dinamically, this means the keys
I have a service which returns a Json data structure. One of the properties
i have json data like this {GetStudentDetails: {TotalCount:5, RootResults:[ {city:West Chester,country:USA,state:PA ,student_id:100}, {city:Philly,country:USA,state:PA,student_id:101}, {city:Buffalo,country:USA,state:NY,student_id:102},
I have a JSON data like this: { hello: { first:firstvalue, second:secondvalue }, hello2:
I have a JSON array like below: var jsonArray = [{k1:v1},{k2:v2},{k3:v3},{k4:v4},{k5:v5}] I don't know
I have JSON text that looks like this: { ok: true, totalPages: 256, arReports:
I have a JSON which looks like this { secInfo: [ { loginResult: 1,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.