使用 $.each 对 Json 数据进行 jquery 循环 [英] jquery loop on Json data using $.each
问题描述
我在名为 data 的变量中返回了以下 JSON.
这是返回的 JSON...
<预><代码>[{"Id": 10004, "PageName": "club"},{"Id": 10040, "PageName": "qaz"},{"Id": 10059, "PageName": "jjjjjjj"}]我正在尝试使用 $.each 循环遍历集合,但我遇到了警报显示未定义的问题.我尝试了很多不同的语法,但似乎无法解决这个问题.
我使用的 JQuery 是
$.each(data, function(i, item) {警报(项目.页面名称);});
有人能指出我正确的方向吗?
编辑这是我用来获取数据的代码
$.getJSON('/Cms/GetPages/123', null, function(data) {填充选择(数据);});
这是回调时调用的函数
function fillSelect(data) {警报(数据);$.each(data, function(i, item) {警报(项目.页面名称);});}
编辑 2这让我有点困惑,根据文档,它应该像我一样工作,但事实并非如此.根据提琴手的说法,标题显示:-
Content-Type: application/json;字符集=utf-8
上面的 JSON 是完全正确的.如果这有什么不同,我正在使用 chrome.将在 IE 和 FF 中测试....
编辑 3
使用 $.get 产生
"[
{
"Id": 10041,
"PageName": "01234567890",
"MetaId": 1000,
"TemplateId": 2
},
{
"Id": 10001,
"PageName": "about",
"MetaId": 1000,
"TemplateId": 1
},
{
"Id": 10056,
"PageName": "fdgdfgdfg",
"MetaId": 1000,
"TemplateId": 1
},
{
"Id": 10052,
"PageName": "hjkhjk",
"MetaId": 1000,
"TemplateId": 2
},
{
"Id": 10059,
"PageName": "jjjjjjj",
"MetaId": 1000,
"TemplateId": 1
},
{
"Id": 10057,
"PageName": "qqqqq",
"MetaId": 1000,
"TemplateId": 2
},
{
"Id": 10054,
"PageName": "qwqw",
"MetaId": 1000,
"TemplateId": 2
}
]"
var data = [{"Id": 10004, "PageName": "club"},{"Id": 10040, "PageName": "qaz"},{"Id": 10059, "PageName": "jjjjjjj"}];$.each(data, function(i, item) {警报(数据[i].PageName);});$.each(data, function(i, item) {警报(项目.页面名称);});
这两个选项效果很好,除非你有类似的东西:
var data.result = [{"Id": 10004, "PageName": "club"},{"Id": 10040, "PageName": "qaz"},{"Id": 10059, "PageName": "jjjjjjj"}];$.each(data.result, function(i, item) {警报(data.result[i].PageName);});
试试这个并描述结果
$.get('/Cms/GetPages/123', function(data) {警报(数据);});
对于编辑 3:
这解决了问题,但不是使用eval"的想法,您应该看到/Cms/GetPages/123"中的响应如何.
$.get('/Cms/GetPages/123', function(data) {$.each(eval(data.replace(/[
]/, "")), function(i, item) {警报(项目.页面名称);});});
I have the following JSON returned in a variable called data.
THIS IS THE JSON THAT GETS RETURNED...
[
{"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.
The JQuery I am using is
$.each(data, function(i, item) {
alert(item.PageName);
});
Can any one point me in the right direction?
EDIT This 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 2 This 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:-
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....
EDIT 3
using $.get produces
"[
{
"Id": 10041,
"PageName": "01234567890",
"MetaId": 1000,
"TemplateId": 2
},
{
"Id": 10001,
"PageName": "about",
"MetaId": 1000,
"TemplateId": 1
},
{
"Id": 10056,
"PageName": "fdgdfgdfg",
"MetaId": 1000,
"TemplateId": 1
},
{
"Id": 10052,
"PageName": "hjkhjk",
"MetaId": 1000,
"TemplateId": 2
},
{
"Id": 10059,
"PageName": "jjjjjjj",
"MetaId": 1000,
"TemplateId": 1
},
{
"Id": 10057,
"PageName": "qqqqq",
"MetaId": 1000,
"TemplateId": 2
},
{
"Id": 10054,
"PageName": "qwqw",
"MetaId": 1000,
"TemplateId": 2
}
]"
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:
this corrects the problem, but not the idea to use "eval", you should see how are the response in '/Cms/GetPages/123'.
$.get('/Cms/GetPages/123', function(data) {
$.each(eval(data.replace(/[
]/, "")), function(i, item) {
alert(item.PageName);
});
});
这篇关于使用 $.each 对 Json 数据进行 jquery 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!