JavaScript eval() JSON 问题

发布于 2024-09-19 08:44:07 字数 1874 浏览 9 评论 0原文

我有下面的 JSON 字符串:

{ "name":"Ruby on Rails 棒球 泽西岛", "价格":"19.99",
"id":"1025786064",
"image":"http://127.0.0.1:3001/assets /products/4/product/ror_baseball.jpeg” }, { "name":"Ruby on Rails 棒球 泽西岛", "价格":"19.99",
"id":"1025786064",
"image":"http://127.0.0.1:3001/assets /products/5/product/ror_baseball_back.jpeg” }, { "name":"Ruby on Rails 铃声 T恤”,“价格”:“19.99”,
"id":"187438981",
"image":"http://127.0.0.1:3001/assets /products/9/product/ror_ringer.jpeg” }, { "name":"Ruby on Rails 铃声 T恤”,“价格”:“19.99”,
"id":"187438981",
"image":"http://127.0.0.1:3001/assets /products/10/product/ror_ringer_back.jpeg” }, { "name":"阿帕奇棒球 泽西岛", "价格":"19.99",
"id":"706676762",
"image":"http://127.0.0.1:3001/assets /products/1004/product/apache_baseball.png” }, { "name":"红宝石棒球球衣", "价格":"19.99", "id":"569012001", "image":"http://127.0.0.1:3001/assets /products/1008/product/ruby_baseball.png” }

在 jQuery 中:

var d = eval("(" + data + ")"); //data is the json string above.

$.each(d, function(idx, item) {
 alert(item);      
});

没有错误,但它只显示第一个序列的数据。如何循环遍历所有数据?

谢谢。

I have the JSON string below :

{ "name":"Ruby on Rails Baseball
Jersey", "price":"19.99",
"id":"1025786064",
"image":"http://127.0.0.1:3001/assets/products/4/product/ror_baseball.jpeg"
}, { "name":"Ruby on Rails Baseball
Jersey", "price":"19.99",
"id":"1025786064",
"image":"http://127.0.0.1:3001/assets/products/5/product/ror_baseball_back.jpeg"
}, { "name":"Ruby on Rails Ringer
T-Shirt", "price":"19.99",
"id":"187438981",
"image":"http://127.0.0.1:3001/assets/products/9/product/ror_ringer.jpeg"
}, { "name":"Ruby on Rails Ringer
T-Shirt", "price":"19.99",
"id":"187438981",
"image":"http://127.0.0.1:3001/assets/products/10/product/ror_ringer_back.jpeg"
}, { "name":"Apache Baseball
Jersey", "price":"19.99",
"id":"706676762",
"image":"http://127.0.0.1:3001/assets/products/1004/product/apache_baseball.png"
}, { "name":"Ruby Baseball Jersey",
"price":"19.99", "id":"569012001",
"image":"http://127.0.0.1:3001/assets/products/1008/product/ruby_baseball.png"
}

Then in jQuery:

var d = eval("(" + data + ")"); //data is the json string above.

$.each(d, function(idx, item) {
 alert(item);      
});

There is no error, but it only shows the first sequence's data. How can I loop through all the data?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

一个人练习一个人 2024-09-26 08:44:08

首先,确保您使用此处的 JSON 解析器: https://github .com/douglascrockford/JSON-js/blob/master/json2.js

那么你的代码将看起来像这样:

var myArrayOfObjects = JSON.parse("[" + data + "]");
for (obj in myArrayOfObjects)
{
   alert("Name:" + myArrayOfObjects[obj].name);
}

或者在 Jquery 中:

$.each(myArrayOfObjects , function(i, o) {
 alert("Name:" + o.name);      
});

Firstly, make sure you use JSON parser from here: https://github.com/douglascrockford/JSON-js/blob/master/json2.js

then your code will look somewhat like this:

var myArrayOfObjects = JSON.parse("[" + data + "]");
for (obj in myArrayOfObjects)
{
   alert("Name:" + myArrayOfObjects[obj].name);
}

or in Jquery:

$.each(myArrayOfObjects , function(i, o) {
 alert("Name:" + o.name);      
});
饮湿 2024-09-26 08:44:08

使用 eval() 解析 JSON 是不安全的。尝试使用浏览器本机(不需要库!)函数 JSON.parse(),该函数在所有浏览器上实现并且安全。

Using eval() to parse JSON is unsafe. Try using the browser-native (no libraries needed!) function JSON.parse() instead, which is implemented across all browsers and is secure.

放低过去 2024-09-26 08:44:08

Crockford 建议不要使用 eval 来解析 JSON,因此您应该使用 JSON.parse 函数。

您可以在此处使用它:

http://www.json.org/js.html

Crockford advises against using eval to parse JSON, so you should be using the JSON.parse function.

Here is where you would use it:

http://www.json.org/js.html

沉鱼一梦 2024-09-26 08:44:08

尝试:

var d = eval("[" + data + "]");

JSON 数组用方括号括起来,例如:'[ 1,2,3 ]'

(是的,不要使用 eval ,而是使用 JSON 内置对象或类似的、更安全的解决方案)

Try:

var d = eval("[" + data + "]");

JSON arrays are wrapped in square brackets, e.g.: '[ 1,2,3 ]'

(Yes, and don't use eval but the JSON built-in object or similar, safer solutions)

扎心 2024-09-26 08:44:08

尝试用 [ ] 包装您的 JSON

您可以在此处验证您的 JSON:JSONLint Validator 以了解在哪里问题就出在。

Try wrapping your JSON with [ ]

You can validate your JSON here: JSONLint Validator to get some sense as to where the problem lies.

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