Convert JSON string to an array in JavaScript - Stack Overflow

I'm using jqPlot to create a chart. The data points for the chart e from a JSON object which is bu

I'm using jqPlot to create a chart. The data points for the chart e 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.

I'm using jqPlot to create a chart. The data points for the chart e 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 edited Dec 27, 2012 at 19:56 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Nov 5, 2012 at 15:52 sardosardo 2,8513 gold badges20 silver badges18 bronze badges 1
  • It should be noted that the definition of JSON is "String representation of javascript array or object". – slebetman Commented Nov 5, 2012 at 16:03
Add a ment  | 

3 Answers 3

Reset to default 8

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.

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.

Take a look at: https://github./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 remended that you parse it through a JSON engine in case there's some bad things in there!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743725165a4496491.html

相关推荐

  • Convert JSON string to an array in JavaScript - Stack Overflow

    I'm using jqPlot to create a chart. The data points for the chart e from a JSON object which is bu

    6天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信