There is a JSON string in a specific format:
[{"A":"SomeStringData","B":1},
{"A":"SomeStringData","B":2},
...
...
...]
- It should be noted that this JSON string passes through all online parsers successfully and is considered valid.
An attempt is being made to create a chart using d3 and nv.d3 by passing the data. The code snippet being used is as follows:
var jsonString; nv.addGraph(function(){ var chart=nv.models.discreteBarChart().x(Something).y(Something); d3.select('#location').datum(jsonString).call(chart); return chart; });
However, simply passing the JSON text to the datum() function does not work.
- Attempts with json.parse(jsonString) and eval have failed as well.
To make it work, a root node was added to the JSON string as shown below:
[{values:[{"A1":"SomeStringData","A2":1}, {"B1":"SomeStringData","B2":2}, ... ... ...]}]
The modified JSON structure causes errors when passed through online parsers.
- Nevertheless, using eval("("+jsonString+")") worked, while JSON.parse() continued to fail.
Concerns about the use of eval() being deemed dangerous prompted an exploration into using JSON.parse(), which also proved unsuccessful.
Are there any insights on what might be going wrong with JSON.parse()? This challenge within the realm of JSON is proving quite perplexing for someone new to it.
In case it helps, the MVC controller receives the JSON string as a variable of type string:
The following function sourced from here
public static class JSONHelper
{
public static string ToJSON(this object obj)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(obj);
}
public static string ToJSON(this object obj, int recursionDepth)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
serializer.RecursionLimit = recursionDepth;
return serializer.Serialize(obj);
}
}
The resultant string is then passed to the controller as a simple string variable. No complex operations are involved in this process.