如何将JSONString转换为Javascript对象 [英] how to convert JSONString to Javascript Object
问题描述
我需要一些有关JavaScript的帮助.我在单击按钮时进行了ajax调用,该调用返回了下面的字符串,这是使用GSON生成的(基本上是JSON对象).
I need some help on the JavaScript. I do a ajax call on a button click, which returns the below String and this is generated using GSON(basically it is a JSON object).
{
"chart":{
"renderTo":"container",
"type":"bar"
},
"title":{
"text":"Engagement Per Vendor Per GBP"
},
"subtitle":{
"text":"ASPT"
},
"xAxis":{
"categories":[
"A",
"B",
"C",
"D"
],
"title":{
"text":"Engagement Per Vendor Per GBP"
}
},
"yAxis":{
"min":0,
"title":{
"text":"Count",
"align":"high"
}
},
"plotOptions":{
"bar":{
"dataLabels":{
"enabled":true
}
}
},
"legend":{
"layout":"vertical",
"align":"right",
"verticalAlign":"bottom",
"x":-100,
"y":100,
"floating":true,
"borderWidth":1,
"backgroundColor":"#FFFFFF",
"shadow":true
},
"credits":{
"enabled":true
},
"series":[
{
"name":"ABC",
"data":[
10,
20,
20,
30
]
},
{
"name":"DEF",
"data":[
10,
20,
30,
40
]
},
{
"name":"GHIJ",
"data":[
20,
30,
40,
10
]
},
{
"name":"KLMN",
"data":[
10,
40,
20,
30
]
}
]
}
当我在JavaScript中获取此数据时. 我正在尝试使用以下语句将对象转换为JSON
When I get this data in my javascript. I’m trying to convert the object to JSON using the below statement
var jsonObj = eval(xmlHttp.responseText);
xmlHttp.responseText
具有以下字符串
{"chart":{"renderTo":"container","type":"bar"},"title":{"text":"Engagement Per Vendor Per GBP"},"subtitle":{"text":"ASPT"},"xAxis":{"categories":["A","B","C","D"],"title":{"text":"Engagement Per Vendor Per GBP"}},"yAxis":{"min":0,"title":{"text":"Count","align":"high"}},"plotOptions":{"bar":{"dataLabels":{"enabled":true}}},"legend":{"layout":"vertical","align":"right","verticalAlign":"bottom","x":-100,"y":100,"floating":true,"borderWidth":1,"backgroundColor":"#FFFFFF","shadow":true},"credits":{"enabled":true},"series":[{"name":"ABC","data":[10,20,20,30]},{"name":"DEF","data":[10,20,30,40]},{"name":"GHIJ","data":[20,30,40,10]},{"name":"KLMN","data":[10,40,20,30]}]}
当我尝试运行jsp时,它在var jsonObj = eval(xmlHttp.responseText);
When I try to run the jsp, it stops at var jsonObj = eval(xmlHttp.responseText);
我已经做过很多次了,但是这次数据有所不同. JSON字符串是通过GSON api中的JSON方法创建的.
I have done this before many time, but this time the data is different. The JSON string was created by by a JSON method from GSON api.
除非将其放入JSON对象,否则将无法对其进行任何操作.对此有任何帮助.
Unless I get this in to a JSON object, I’ll not be able to do anything with it. Any help on this is appreaciated.
关于, 森尼
推荐答案
jQuery可以做到:
jQuery can do it:
http://api.jquery.com/jQuery.parseJSON/
如果您不想使用eval
,则可以使用一个独立的解析器: https://github.com/douglascrockford/JSON-js
If you don't want to use eval
, here is a standalone parser: https://github.com/douglascrockford/JSON-js
更新只需使用内置的 JSON对象
这篇关于如何将JSONString转换为Javascript对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!