JavaScript eval() JSON 问题
我有下面的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先,确保您使用此处的 JSON 解析器: https://github .com/douglascrockford/JSON-js/blob/master/json2.js
那么你的代码将看起来像这样:
或者在 Jquery 中:
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:
or in Jquery:
使用
eval()
解析 JSON 是不安全的。尝试使用浏览器本机(不需要库!)函数JSON.parse()
,该函数在所有浏览器上实现并且安全。Using
eval()
to parse JSON is unsafe. Try using the browser-native (no libraries needed!) functionJSON.parse()
instead, which is implemented across all browsers and is secure.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
尝试:
JSON 数组用方括号括起来,例如:'[ 1,2,3 ]'
(是的,不要使用
eval
,而是使用JSON
内置对象或类似的、更安全的解决方案)Try:
JSON arrays are wrapped in square brackets, e.g.: '[ 1,2,3 ]'
(Yes, and don't use
eval
but theJSON
built-in object or similar, safer solutions)尝试用 [ ] 包装您的 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.