score:0
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.
score:5
from JSON format definition here http://json.org/ 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.
score:7
I was trying to accomplish 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.net library (using JsonConvert and JRaw). http://james.newtonking.com/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>)
Source: stackoverflow.com
Related Query
- Serializing a function as a parameter in json using C#
- Parsing JSON for use with Highcharts using jquery .parseJSON or JSON.parse: how to remove quotes from function calls for formatters?
- Formatting JSON data monthwise for HighCharts using MySQL
- Pass Data String as parameter to a Function for HighCharts
- Color contrast setting on dataLabels style parameter using Highcharts 5 is not working on some colors
- Using PhantomJS to create HighCharts grahps server side for use in PDF creation (PHP) - results in exit code 11 from PHPs exec();
- Uncaught TypeError: undefined is not a function when using highcharts
- how to sort and select top 5 elements in a json array using angular JS
- create a donut chart using highchart using jquery json object
- using angular directive to draw highchart pie chart but when i am using it in success function it is not working
- Passing a JavaScript function from JSON encoded PHP
- Highcharts - Using a function to change the tooltip
- Sending javascript function with JSON is possible?
- How To Call A Function To Make A Graph Using HighCharts in PHP
- How to Populate Highchart using Json Object
- Place Data in Highchart using json and post url
- Using global data in High Charts point click custom function in R Shiny
- JSON data not showing in highstock Candlestick chart using PHP & MYSQL
- Highcharts Solid Gauge Dynamic Update Using JSON
- Highcharts: Using JSON to load xAxis data as 'datetime'?
- Dotnet Highchart Using JSON
- passing json values to highcharts from .net code behind
- Charts using Highcharts with multiple series from JSON
- passing formatting JavaScript code to HighCharts with JSON
- Highcharts Dynamic Drilldown using json
- Plot Highchart multiple series line chart using JSON data
- Setting Min and Max values for more than two Highchart Solid Gauges using function
- Highchart is not getting populated using external json data
- Highcharts display label for pie chart using html table as data source
- Error using Highcharts (>= 8.0.1) function stockChart() in Angular
More Query from same tag
- Change the Size of the Export Button In Fusion Chart in PHP
- Highcharts legend custom css style / format using only highcharts options
- How to make - - legend in 'areaspline' type high charts?
- display different series data in single line of Stacked Bar using highcharts
- highcharts: issues
- Spiderweb chart with different axis ranges (parallel-coordinates)
- How to make the highchart fully occupied with the div that had set?
- column charts first date cut issue
- DotNet.Highcharts , Is there any way to use it in Winform application?
- Highchart plot points are being displayed incorrectly
- Highcharts - series.remove(); 4 series in chart, only 2 removed
- How do I put Icons in the y Axis for a Dynamic Highcharts chart?
- How to disable label from Highchart
- How to calculate the height of bar chart
- Highchart range selector doesn't show up
- Add a custom text tooltip that differs from point to point in Highcharts
- highcharts series update in javascript
- change distance of x-axis labels from axis in highchart
- Memory heap increases when Chrome tab is inactive
- Highcharts Grid Line Width (or Color) - Single Line
- How to redraw highchart series with json data value?
- Highcharts conflict with extjs
- Android highcharts how to enable or disable data labels
- Highcharts columnrange tooltip issue
- Highchart polar with additional pie series below
- Enable Drilldown in highchart without modifying the series
- Javascript datepicker date defaulting to 01-01-1970 HighCharts
- Single column HighCharts for an array
- highcharts crosshairs wont work
- Colors are not rotating in Highcharts Bubble chart