开发者

how do parse JSON in javascript?

This is my json string

"[{"/stab/cg/{4C开发者_高级运维D742B1-C1CA-4708-BE78-0FCA2EB01A86}/TOPS_00":[{"key":"C0.A8.01.06","value":"31"},{"key":"C0.A8.50.01","value":"25"},{"key":"C0.A8.50.81","value":"22"},{"key":"E0.00.00.FC","value":"19"},{"key":"C0.A8.01.FF","value":"18"},{"key":"C0.A8.50.FF","value":"18"},{"key":"4A.7D.EC.5F","value":"11"},{"key":"4A.7D.EC.4E","value":"11"},{"key":"SYS:GROUP_TOTALS","value":"158"}]}]"

after eval('('+ evt.data + ')'), i need to get like this

["/stab/cg/{4CD742B1-C1CA-4708-BE78-0FCA2EB01A86}/TOPS_00",[{"key":"C0.A8.01.06","value":"31"},{"key":"C0.A8.50.01","value":"25"},{"key":"C0.A8.50.81","value":"22"},{"key":"E0.00.00.FC","value":"19"},{"key":"C0.A8.01.FF","value":"18"},{"key":"C0.A8.50.FF","value":"18"},{"key":"4A.7D.EC.5F","value":"11"},{"key":"4A.7D.EC.4E","value":"11"},{"key":"SYS:GROUP_TOTALS","value":"158"}]]

How can i get this using javascript?


If you can utilize jQuery you can use the $.parseJSON() method. Documentation here

For prototype use the .evalJSON() method. Documentation here


JSON.parse has a native implementation in most modern browsers and you can shim it using the implementation on the JSON homepage that Quentin indicated.


Don't use eval for this. A collection of libraries for parsing JSON is listed near the end of the JSON homepage, there are a couple for JavaScript including json2.js which is the usual choice.

Manipulating the data structure has nothing to do with parsing JSON though. If you really want to transform it like then then you'd want something like (untested):

var newObj = [];
for (keys) in myObj) {
    newObj.push([key].concat(myObj[key])
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜