最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Serializing a function as a parameter in json using C# - Stack Overflow

programmeradmin3浏览0评论

I am trying to create the json needed to create an object in jQuery using C#. The json needed is

{
    title: 'title text',
    upperVal: 40,
    lowerVal: 5,
    mouseover: function() { return 'difference ' + (upperVal - lowerVal); }
}

The first few elements were simple enough. I created a class to represent the object, JSObj, and then run this through JavascriptSerializer.Serialize()

public class JSObj {
    public string title { get; set; }
    public int upperVal { get; set; }
    public int lowerVal { get; set; }
}

This works fine for the first few attributes, but I don't have a clue how to return the correct mouseover function.

EDIT: The code provided is just sample code because the structure of the json I'm actually using is a bit more plicated. I'm using HighCharts, and one of the config options that I really need to use requires a function, even though they are not really valid json ( ) so unfortunately I can't avoid the problem

I am trying to create the json needed to create an object in jQuery using C#. The json needed is

{
    title: 'title text',
    upperVal: 40,
    lowerVal: 5,
    mouseover: function() { return 'difference ' + (upperVal - lowerVal); }
}

The first few elements were simple enough. I created a class to represent the object, JSObj, and then run this through JavascriptSerializer.Serialize()

public class JSObj {
    public string title { get; set; }
    public int upperVal { get; set; }
    public int lowerVal { get; set; }
}

This works fine for the first few attributes, but I don't have a clue how to return the correct mouseover function.

EDIT: The code provided is just sample code because the structure of the json I'm actually using is a bit more plicated. I'm using HighCharts, and one of the config options that I really need to use requires a function, even though they are not really valid json ( http://www.highcharts./ref/#tooltip--formatter ) so unfortunately I can't avoid the problem

Share Improve this question edited Aug 20, 2015 at 18:52 ragnarswanson 3153 silver badges10 bronze badges asked Jul 12, 2011 at 11:18 Jordan WallworkJordan Wallwork 3,1142 gold badges25 silver badges51 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 8

I was trying to acplish something similar. In my case I was using MVC Razor syntax trying to generate a json object with a function passed in using the @<text> syntax.

I was able to get the desired output using the Json library (using JsonConvert and JRaw). http://james.newtonking./projects/json/help/html/SerializeRawJson.htm

Example:

public class JSObj
{
    public string Title { get; set; }
    public int UpperVal { get; set; }
    public int LowerVal { get; set; }
    public object MouseOver
    {
        get
        {
            // use JRaw to set the value of the anonymous function
            return new JRaw(string.Format(@"function(){{ return {0}; }}", UpperVal - LowerVal));
        }
    }
}

// and then serialize using the JsonConvert class
var obj = new JSObj { Title = "Test", LowerVal = 4, UpperVal = 10 };
var jsonObj = JsonConvert.SerializeObject(obj);

That should get you the json object with the function (instead of the function in a string).

{"Title":"Test","UpperVal":10,"LowerVal":4,"MouseOver":function(){ return 6; }}

Post: How to serialize a function to json (using razor @<text>)

from JSON format definition here http://json/ it is clear that you cannot have functions in JSON.

define your function elsewhere in application and call it explicitly.

any kind of hack to support functions in JSON is bad practice as it conflicts with purpose of JSON as "lightweight data-interchange format". you can't interchange functions as they cannot be understood by anything else except javascript.

The JSON format cannot represent functions like that (directly). You could encode it as a string and then instantiate the function explicitly in the client, or pass the name of a function that's already present at the client.

发布评论

评论列表(0)

  1. 暂无评论