在 JavaScript 中将 JSON 字符串转换为数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13235614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 18:09:24  来源:igfitidea点击:

Convert JSON string to an array in JavaScript

javascriptarraysjsonstringjqplot

提问by sardo

I'm using jqPlotto 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:

我正在使用jqPlot创建图表。图表的数据点来自使用 GSON 构建的 JSON 对象。图表数据点以 JavaScript 数组格式构建,因此保存要发送给客户端的数据的 Java 对象按如下方式存储数据:

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.

x 点是日期。

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.

是否可以直接从 JSON 对象传递这些数据,就像它是一个 JavaScript 数组一样?目前 MyJsonObject.chartDataPoints 被视为字符串,因此 jqPlot ($.jqplot ('chart1', MyJsonObject.chartDataPoints) 不会绘制任何内容。

回答by Mitch Satchwell

One option is to use eval:

一种选择是使用eval

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

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

以上是执行此操作的最简单且最受支持的方法,但只有eval在您信任源时才应使用,因为它会执行任何 JavaScript 代码。

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

某些浏览器具有本机 JSON 解析器,在这种情况下,您可以执行以下操作:

var arr = JSON.parse(json_text);

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

jQuery等第三方库也可以提供处理 JSON 的功能。在 jQuery 中,您可以执行以下操作:

var arr = jQuery.parseJSON(json_text);

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

最底层的两种方法(使用解析器)是首选,因为它们提供了一定程度的保护。

回答by Joshua Dwire

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.

如果删除引号,则这是一个有效的数组声明。然后你可以循环遍历将 x 点转换为日期的数组。

回答by RemarkLima

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

看一看: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!

有一种方法可以将字符串解析为对象,这是一种“安全”方法 - 您可以eval(chartDataPoints)在 javascript 中执行 an ,但始终建议您通过 JSON 引擎解析它,以防其中有一些不好的东西!