jquery loop on Json data using $.each
Hi 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 alot 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
"[\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]"
Answer:
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(/[\r\n]/, "")), function(i, item) {
alert(item.PageName);
});
});
jquery loop on Json data using $.each的更多相关文章
- jQuery loop over JSON字符串 – $.each实例
先来一段简单的javascript对象的遍历: var json = [ {"id":"1","tagName":"apple&q ...
- jQuery.parseJSON vs JSON.parse
转载:http://stackoverflow.com/questions/10362277/jquery-parsejson-vs-json-parse 一.JavaScript函数JSON.par ...
- javascript: Jquery each loop with json array or object
http://www.codeproject.com/Articles/779303/JSON-and-Microsoft-technologies http://www.codeproject.co ...
- jQuery解析JSON出现SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data
SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data 我在使用$.parseJSON解析后 ...
- jquery 如何遍历json
var obj = {"status":1,"bkmsg":"\u6210\u529f","bkdata":[" ...
- JQuery ajax返回JSON时的处理方式
最近在使用JQuery的ajax方法时,要求返回的数据为json数据,在处理的过程中遇到下面的几个问题,那就是采用不同的方式来生成json数据的时候,在$.ajax方法中应该是如何来处理的,下面依次来 ...
- 关于Jquery中ajax方法data参数用法的总结
data 发送到服务器的数据.将自动转换为请求字符串格式.GET 请求中将附加在 URL 后.查看 processData 选项说明以禁止此自动转换.必须为 Key/Value 格式.如果为数组,jQ ...
- jQuery 中使用 JSON
转载:http://www.cnblogs.com/haogj/archive/2011/12/01/2271098.html JSON 格式 json 是 Ajax 中使用频率最高的数据格式,在浏览 ...
- jquery ajax返回json数据进行前后台交互实例
jquery ajax返回json数据进行前后台交互实例 利用jquery中的ajax提交数据然后由网站后台来根据我们提交的数据返回json格式的数据,下面我来演示一个实例. 先我们看演示代码 代码如 ...
随机推荐
- noi.ac NOIP2018 全国热身赛 第四场 T2 sort
[题解] 跟51nod 1105差不多. 二分答案求出第L个数和第R个数,check的时候再套一个二分或者用two pointers. 最后枚举ai在b里面二分,找到所有范围内的数,排序后输出. 注意 ...
- github私有库购买信息
github私有库购买信息 一年84美元. 换算成人民币是:532元. 话说其他开发者都买了么?
- Linux 下eval命令揭秘
eval 命令中文意思是“重新计算求出参数内容”,该命令大致有以下几个作用 将变量名替换后并执行 beautifulsoup4- Desktop Documents hc.war lxml- mybl ...
- BGP路由属性详解
Weight属性:cisco私有的BGP属性参数,它只适用于一台路由器中的路由,也就是不会传递给任何其他的路由器.他的取值范围为<0-65535>,这个数越大优先级越高,默认从邻居学到的路 ...
- 【枚举】Southwestern Europe Regional Contest H - Sheldon Numbers
https://vjudge.net/contest/174235#problem/H [题意] 求[x,y]之间有多少个Sheldon Number Sheldon Number是二进制满足以下条件 ...
- idea web项目启动失败的情况---webapp文件夹路径不对,应如图位置
- POJ 1017 Packet
http://poj.org/problem?id=1017 有1*1 2*2...6*6的物品 要装在 6*6的parcel中 问最少用多少个parcel 一直没有找到贪心的策略 问题应该出现在 总 ...
- springmvc和dubbo整合时,不配置spring listener报错找不到/WEB-INF/config/applicationContext.xml
原因,dubbo2.6.3版本开始就需要先在listener中配置容器,否则报错,2.6.2版本则不需要
- iOS present出一个背景为半透明的试图
WDKChatRoomViewController *roomVC = [[WDKChatRoomViewController alloc] init]; roomVC.titleStr = [gro ...
- oc温习五:字符串
/** substringFromIndex: --从第from位数 开始截取字符串 */ NSString *str = @"asdfghjkzxcbnm"; NSString ...