如何在JavaScript中解析JSON以获取值
我真的很困于解析 JSON 字符串并获取它的值。我得到了 JSON 字符串作为
{"user":{"id":"1","firstname":"Freelogin","created":"0000-00-00 00:00:00","lastname":"Administrator","email":"[email protected]", "usergroup_id":"1","status":"1","ip_enable":"N","priv":"0","expire":""},"data":{ "1":{"5":{"last_update":"2010-12-13 16:16:16","status":"0"},"3":{"last_update":"2010-12-13 16:41:48","status":"1"}},"2":{"6":{"last_update":"2010-12-13 16:41:48","status":"1"}}},"server_array":[{"id":"1","name":"anes.yyy.net"},{ "id":"2","name":"neseema.xxx.net"}],"service_array":[{"id":"5","name":"POP3"}, {"id":"6","name":"Cpanel"},{"id":"3","name":"SMTP"}],"sort_by":"servername", "sort_order":"ASC","pagelinks":"","totrows":"2","offset":"0","limitvalue":"10", "rows_monitor":2,"current":"monitor","uri":false}
如何解析它并进一步获取结果 JavaScript 中的处理
I am really stuck in parsing a JSON string and take it's values. I got the JSON string as
{"user":{"id":"1","firstname":"Freelogin","created":"0000-00-00 00:00:00","lastname":"Administrator","email":"[email protected]", "usergroup_id":"1","status":"1","ip_enable":"N","priv":"0","expire":""},"data":{ "1":{"5":{"last_update":"2010-12-13 16:16:16","status":"0"},"3":{"last_update":"2010-12-13 16:41:48","status":"1"}},"2":{"6":{"last_update":"2010-12-13 16:41:48","status":"1"}}},"server_array":[{"id":"1","name":"anes.yyy.net"},{ "id":"2","name":"neseema.xxx.net"}],"service_array":[{"id":"5","name":"POP3"}, {"id":"6","name":"Cpanel"},{"id":"3","name":"SMTP"}],"sort_by":"servername", "sort_order":"ASC","pagelinks":"","totrows":"2","offset":"0","limitvalue":"10", "rows_monitor":2,"current":"monitor","uri":false}
How to Parse this and take the Results for further
processing in JavaScript
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您应该使用
jQuery.parseJSON
。如果可用,它将使用 原生 JSON,并且仅在必要时使用eval
,经过健全性检查后。You should use
jQuery.parseJSON
. It will use native JSON if available, and only useeval
if necessary, after a sanity check.使用 JSON.parse (重定向来自 http://json.org),或者 MDN
Use JSON.parse (redirected from http://json.org), alternatively MDN
Json 已经是一些 javascript 了。所以解析只是使用
eval
,例如:
jquery 还有一些函数
jquery.parseJSON
就像:
Jquery 更好,因为它会进行一些检查并执行得更好。
Json is already some javascript. so parsing is just using
eval
like:
Also jquery has some function for it
jquery.parseJSON
like:
Jquery is better because it would make some checks and perform better.
首先,下载 jQuery。
其次,将其包含在您的页面中。
第三,如果您的变量是这样的:
那么,
将为您提供可供操作的所需解析对象。
我在 JSONLint 上尝试了您的 JSON 字符串,它说它是有效的,所以您应该没有问题。
First, download jQuery.
Second, include it in your page.
Third, if your variable is this:
then,
will give you the desired parsed object that's ready for manipulation.
I tried out your JSON string on JSONLint and it says it's valid, so you should have no problems with it.
你可能在 som String 变量中得到了 json,
现在你可以通过 jQuery 轻松解析它(你也可以通过本机 javaScript eval 解析它,但是存在一些安全问题,格式错误的输入字符串 fe,这是由 jQuery 覆盖的,而不是在 eval 中) )
现在您可以轻松访问您的 json 对象
you probably got your json in som String variable
now you can easily parse it via jQuery (you also can parse it via native javaScript eval, but there are some security issues, badly formated input string f.e., that is covered with jQuery and not in eval)
Now you can easily acces your json object
你不需要 jQuery,在 ECMAScript5 中,原生支持 JSON 对象,使用它你可以使用 JSON.parse 方法将字符串解析为 JS 对象。 IE9 将支持 ES5 和 FF,Chrome 已经支持。
目前,您可以使用 json2.js (您可以在此处查看源代码)作为后备本身不支持 JSON 的浏览器。
You don't need jQuery, in ECMAScript5 JSON object will be supported natively and with it you can use JSON.parse method to parse a string into a JS object. IE9 will support ES5 and FF and Chrome already do.
For the moment you can use json2.js (you can look at the source here) as fallback for the browsers that don't support JSON natively.