使用 $.each 对 Json 数据进行 jquery 循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2342371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery loop on Json data using $.each
提问by Rippo
I have the following JSON returned in a variable called data.
我在名为 data 的变量中返回了以下 JSON。
THIS IS THE JSON THAT GETS RETURNED...
这是返回的 JSON...
[
{"Id": 10004, "PageName": "club"},
{"Id": 10040, "PageName": "qaz"},
{"Id": 10059, "PageName": "jjjjjjj"}
]
and I am trying to loop through the collection using $.each but I am running into problems where the alert is showing undefined. I have tried a lot of different syntax but can't seem to figure this out.
我正在尝试使用 $.each 遍历集合,但我遇到了警报显示未定义的问题。我尝试了很多不同的语法,但似乎无法解决这个问题。
The JQuery I am using is
我正在使用的 JQuery 是
$.each(data, function(i, item) {
alert(item.PageName);
});
Can any one point me in the right direction?
任何人都可以指出我正确的方向吗?
EDITThis is the code I am using to grab the data
编辑这是我用来获取数据的代码
$.getJSON('/Cms/GetPages/123', null, function(data) {
fillSelect(data);
});
and this is the function that gets called upon call back
这是在回调时调用的函数
function fillSelect(data) {
alert(data);
$.each(data, function(i, item) {
alert(item.PageName);
});
}
EDIT 2This is slightly confusing me, according to the docs it should work as I have it, but it doesn't. According to fiddler the header shows:-
编辑 2这让我有点困惑,根据文档,它应该像我一样工作,但事实并非如此。根据提琴手的说法,标题显示:-
Content-Type: application/json; charset=utf-8
and the JSON is exactly correct above. I am using chrome if this makes any different. Will test in IE and FF....
上面的 JSON 完全正确。如果这有任何不同,我正在使用 chrome。将在 IE 和 FF 中测试....
EDIT 3
编辑 3
using $.get produces
使用 $.get 产生
"[\r\n {\r\n \"Id\": 10041,\r\n \"PageName\": \"01234567890\",\r\n \"MetaId\": 1000,\r\n \"TemplateId\": 2\r\n },\r\n {\r\n \"Id\": 10001,\r\n \"PageName\": \"about\",\r\n \"MetaId\": 1000,\r\n \"TemplateId\": 1\r\n },\r\n {\r\n \"Id\": 10056,\r\n \"PageName\": \"fdgdfgdfg\",\r\n \"MetaId\": 1000,\r\n \"TemplateId\": 1\r\n },\r\n {\r\n \"Id\": 10052,\r\n \"PageName\": \"hjkhjk\",\r\n \"MetaId\": 1000,\r\n \"TemplateId\": 2\r\n },\r\n {\r\n \"Id\": 10059,\r\n \"PageName\": \"jjjjjjj\",\r\n \"MetaId\": 1000,\r\n \"TemplateId\": 1\r\n },\r\n {\r\n \"Id\": 10057,\r\n \"PageName\": \"qqqqq\",\r\n \"MetaId\": 1000,\r\n \"TemplateId\": 2\r\n },\r\n {\r\n \"Id\": 10054,\r\n \"PageName\": \"qwqw\",\r\n \"MetaId\": 1000,\r\n \"TemplateId\": 2\r\n }\r\n]"
回答by andres descalzo
var data = [
{"Id": 10004, "PageName": "club"},
{"Id": 10040, "PageName": "qaz"},
{"Id": 10059, "PageName": "jjjjjjj"}
];
$.each(data, function(i, item) {
alert(data[i].PageName);
});
$.each(data, function(i, item) {
alert(item.PageName);
});
these two options work well, unless you have something like:
这两个选项效果很好,除非你有类似的东西:
var data.result = [
{"Id": 10004, "PageName": "club"},
{"Id": 10040, "PageName": "qaz"},
{"Id": 10059, "PageName": "jjjjjjj"}
];
$.each(data.result, function(i, item) {
alert(data.result[i].PageName);
});
EDIT:
编辑:
try with this and describes what the result
试试这个并描述结果
$.get('/Cms/GetPages/123', function(data) {
alert(data);
});
FOR EDIT 3:
对于编辑 3:
this corrects the problem, but not the idea to use "eval", you should see how are the response in '/Cms/GetPages/123'.
这解决了问题,但不是使用“eval”的想法,您应该看到“/Cms/GetPages/123”中的响应如何。
$.get('/Cms/GetPages/123', function(data) {
$.each(eval(data.replace(/[\r\n]/, "")), function(i, item) {
alert(item.PageName);
});
});
回答by vava
Have you converted your data from string to JavaScript object?
您是否将数据从字符串转换为 JavaScript 对象?
You can do it with data = eval('(' + string_data + ')');
or, which is safer, data = JSON.parse(string_data);
but later will only works in FF 3.5 or if you include json2.js
你可以用data = eval('(' + string_data + ')');
or来做,这更安全,data = JSON.parse(string_data);
但以后只适用于 FF 3.5 或者如果你包含json2.js
jQuery since 1.4.1 also have function for that, $.parseJSON()
.
jQuery 从 1.4.1 开始也有这个功能,$.parseJSON()
.
But actually, $.getJSON()
should give you already parsed json object, so you should just check everything thoroughly, there is little mistake buried somewhere, like you might have forgotten to quote something in json, or one of the brackets is missing.
但实际上,$.getJSON()
应该给你已经解析的 json 对象,所以你应该彻底检查所有内容,某处埋藏着小错误,比如你可能忘记在 json 中引用某些内容,或者缺少一个括号。
回答by kgiannakakis
getJSON will evaluate the data to JSON for you, as long as the correct content-type is used. Make sure that the server is returning the data as application/json.
只要使用正确的内容类型,getJSON 就会为您将数据评估为 JSON。确保服务器将数据作为 application/json 返回。
回答by Rolando Gonzales Medina
$.each(JSON.parse(result), function(i, item) {
alert(item.number);
});