Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm using jqPlot to create a chart. The data points for the chart come from a JSON object which is built using GSON. The chart data points are built in a JavaScript array format, so the Java object that holds the data to send to the client stores the data as follows:

String chartDataPoints = "[[1352128861000, 0.0], [1352128801000, 0.0], [1352128741000,   0.0], [1352128681000, 0.0], [1352128621000, 0.0],...More chart points in this format ,[[x0,y0], [x1,y2]...]]";

The x points are dates.

Is it possible to pass this data straight from the JSON object as though it is a JavaScript array? Currently MyJsonObject.chartDataPoints is treated as a String, and so jqPlot ($.jqplot ('chart1', MyJsonObject.chartDataPoints) doesn't plot anything.

share|improve this question
It should be noted that the definition of JSON is "String representation of javascript array or object". – slebetman Nov 5 '12 at 16:03

3 Answers

up vote 4 down vote accepted

One option is to use eval:

var arr = eval('(' + json_text + ')');

The above is easiest and most widely supported way of doing this, but you should only use eval if you trust the source as it will execute any JavaScript code.

Some browsers have a native JSON parser in which case you can do the following:

var arr = JSON.parse(json_text);

Third-party libraries such as jQuery can also provide functions for dealing with JSON. In jQuery, you can do the following:

var arr = jQuery.parseJSON(json_text);

The bottom two methods (which use a parser) are preferred as they provide a level of protection.

share|improve this answer
adding to MitchS' (+1) answer, IE6 and IE7 are two browsers who don't have JSON object defined. In such case you can use Crockford's json2 script to construct JSON objec. – cbayram Nov 5 '12 at 16:02
I tried JQuery.parseJSON before posting...and I got 'SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data' – sardo Nov 5 '12 at 16:39
Can you show your code? A jsFiddle would be useful. – MitchS Nov 5 '12 at 16:43
I think I've got what I need with the eval() function. JSON.parse() didn't work...but as long as eval() does it doesn't matter. Will the order of the array elements be the same as the original structure? My jQPlot graph isn't plotting correctly, but I think this is because my imports for JQPlot rendering can't be reached. I'm using Wicket and I think it's doing something funny. – sardo Nov 5 '12 at 16:59
Yes, the order will be the same. You can always loop through to check but it should be. – MitchS Nov 5 '12 at 16:59

If you remove the quotation marks, that is a valid array declaration. Then you could cycle through the array converting the x points to dates.

share|improve this answer
How can I remove the quotation marks from MyJsonObject.chartDataPoints so I get [[x0,y0],[x1,y1]....[xn,yn]],[[X0,Y0],[X1,Y1]...[Xn,Yn]]? – sardo Nov 5 '12 at 17:03
@sardo How does the data get there in the first place, is it in the javascript when it is downloaded from the server (inserted by something like php), or is it stored in a variable? – Joshua Dwire Nov 5 '12 at 17:07
The client makes an Ajax request. The server returns a JSON object where chartDataPoints is the name of the variable and the chart data is contained within the value. – sardo Nov 6 '12 at 9:09
So you're actually needing to parse a json string you get from the server? If you can trust the server not to try an xss on you (eg. it's your own server), if you call the eval function with the string, it could return a javascript object. – Joshua Dwire Nov 6 '12 at 11:52
Yep, I'm using the eval() function now. Happy with that (yes it's my own server that builds the JSON object from what it receives from the web service, so I have full control. The other issue I had (graph not plotting properly) was that I need [] surrounding my graph data (can't blame Wicket...not this time anyway :-) ) – sardo Nov 6 '12 at 12:20

Take a look at: https://github.com/douglascrockford/JSON-js

There's a method to parse the string into an object, which is a "safe" method - You can just do an eval(chartDataPoints) in your javascript, however it's always recommended that you parse it through a JSON engine in case there's some bad things in there!

share|improve this answer
Thanks will take a look – sardo Nov 5 '12 at 17:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.