如何在JavaScript中解析JSON以获取值

发布于 2024-10-07 12:49:32 字数 962 浏览 0 评论 0原文

我真的很困于解析 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

格子衫的從容 2024-10-14 12:49:32

您应该使用 jQuery.parseJSON。如果可用,它将使用 原生 JSON,并且仅在必要时使用 eval ,经过健全性检查后。

You should use jQuery.parseJSON. It will use native JSON if available, and only use eval if necessary, after a sanity check.

乖乖公主 2024-10-14 12:49:32

使用 JSON.parse (重定向来自 http://json.org),或者 MDN

Use JSON.parse (redirected from http://json.org), alternatively MDN

酷到爆炸 2024-10-14 12:49:32

Json 已经是一些 javascript 了。所以解析只是使用 eval

例如:

 var foobar = eval(yourjson);
 alert(foobar.user);

jquery 还有一些函数 jquery.parseJSON

就像:

   var foobar = $.parseJSON(yourjson);

Jquery 更好,因为它会进行一些检查并执行得更好。

Json is already some javascript. so parsing is just using eval

like:

 var foobar = eval(yourjson);
 alert(foobar.user);

Also jquery has some function for it jquery.parseJSON

like:

   var foobar = $.parseJSON(yourjson);

Jquery is better because it would make some checks and perform better.

败给现实 2024-10-14 12:49:32

首先,下载 jQuery

其次,将其包含在您的页面中。

第三,如果您的变量是这样的:

var jsonString = '{"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}';

那么,

var parsedJson = jQuery.parseJSON(jsonString);

将为您提供可供操作的所需解析对象。

我在 JSONLint 上尝试了您的 JSON 字符串,它说它是有效的,所以您应该没有问题。

First, download jQuery.

Second, include it in your page.

Third, if your variable is this:

var jsonString = '{"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}';

then,

var parsedJson = jQuery.parseJSON(jsonString);

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.

安穩 2024-10-14 12:49:32

你可能在 som String 变量中得到了 json,

var 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}';

现在你可以通过 jQuery 轻松解析它(你也可以通过本机 javaScript eval 解析它,但是存在一些安全问题,格式错误的输入字符串 fe,这是由 jQuery 覆盖的,而不是在 eval 中) )

result = jQuery.parseJSON(json);

现在您可以轻松访问您的 json 对象

alert('Hello user, your name is ' + json.user.firstname);

you probably got your json in som String variable

var 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}';

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)

result = jQuery.parseJSON(json);

Now you can easily acces your json object

alert('Hello user, your name is ' + json.user.firstname);
绮烟 2024-10-14 12:49:32

你不需要 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文