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的更多相关文章

  1. jQuery loop over JSON字符串 – $.each实例

    先来一段简单的javascript对象的遍历: var json = [ {"id":"1","tagName":"apple&q ...

  2. jQuery.parseJSON vs JSON.parse

    转载:http://stackoverflow.com/questions/10362277/jquery-parsejson-vs-json-parse 一.JavaScript函数JSON.par ...

  3. javascript: Jquery each loop with json array or object

    http://www.codeproject.com/Articles/779303/JSON-and-Microsoft-technologies http://www.codeproject.co ...

  4. 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解析后 ...

  5. jquery 如何遍历json

    var obj = {"status":1,"bkmsg":"\u6210\u529f","bkdata":[" ...

  6. JQuery ajax返回JSON时的处理方式

    最近在使用JQuery的ajax方法时,要求返回的数据为json数据,在处理的过程中遇到下面的几个问题,那就是采用不同的方式来生成json数据的时候,在$.ajax方法中应该是如何来处理的,下面依次来 ...

  7. 关于Jquery中ajax方法data参数用法的总结

    data 发送到服务器的数据.将自动转换为请求字符串格式.GET 请求中将附加在 URL 后.查看 processData 选项说明以禁止此自动转换.必须为 Key/Value 格式.如果为数组,jQ ...

  8. jQuery 中使用 JSON

    转载:http://www.cnblogs.com/haogj/archive/2011/12/01/2271098.html JSON 格式 json 是 Ajax 中使用频率最高的数据格式,在浏览 ...

  9. jquery ajax返回json数据进行前后台交互实例

    jquery ajax返回json数据进行前后台交互实例 利用jquery中的ajax提交数据然后由网站后台来根据我们提交的数据返回json格式的数据,下面我来演示一个实例. 先我们看演示代码 代码如 ...

随机推荐

  1. 关于Python中包裹传参和解包裹的理解

    1.包裹传参 首先思考一个问题:为什么要有包裹传参?原因包括但不仅限于以下两点:①不确定参数的个数.②希望函数定义的更加松散灵活 包裹传参分两种:包裹位置传参和包裹关键字传参.先看包裹位置传参: 在这 ...

  2. 给Django中的url起名字

    url反转  =>reverse 1.from django.shortcuts  import  reverse 2. 利用reverse函数对URL名称进行反转  reverse(url名称 ...

  3. LeetCode(15) 3Sum

    题目 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all ...

  4. Task(TPL)简单的实现Winform(WPF)异步

    很多时候,我们要实现Winform异步操作,你可以用传统的方法,但个人感觉代码不好理解,而且使用真有点不舒服.也可以用Task来实现,Task(.net4.0新添加的对象)其实就是对线程池线程的一个封 ...

  5. 异常 Failed to bind NettyServer on /10.133.7.216:29105, cause: Failed to bind to: /0.0.0.0:29105

    "C:\Program Files\Java\jdk1.7.0_80\bin\java" -agentlib:jdwp=transport=dt_socket,address=12 ...

  6. Android版网易云音乐唱片机唱片磁盘旋转及唱片机机械臂动画关键代码实现思路

     Android版网易云音乐唱片机唱片磁盘旋转及唱片机机械臂动画关键代码实现思路 先看一看我的代码运行结果. 代码运行起来初始化状态: 点击开始按钮,唱片机的机械臂匀速接近唱片磁盘,同时唱片磁盘也 ...

  7. 【优先级队列】Southwestern Europe Regional Contest Canvas Painting

    https://vjudge.net/contest/174235#problem/D [题意] 给定n个已知size的帆布,要给这n块帆布涂上不同的颜色,规则是这样的: 每次选择一种颜色C 对于颜色 ...

  8. 【并查集】F.find the most comfortable road

    https://www.bnuoj.com/v3/contest_show.php?cid=9146#problem/F [题意] 给定n个城市和m条带权边,q次查询,问某两个城市之间的所有路径中最大 ...

  9. POJ3150:Cellular Automaton

    题意看不懂加题目想不通,很菜. n<=500个数围城环,每次操作对每个数Ai把与i在环上相距不超过d<n/2(包括Ai)的数加起来取模m<=1e6,求K<=1e7次操作后的环. ...

  10. JS中的call()和apply()方法区别

    如 果没接触过动态语言,以编译型语言的思维方式去理解javaScript将会有种神奇而怪异的感觉,因为意识上往往不可能的事偏偏就发生了,甚至觉得不可 理喻.如果在学JavaScript这自由而变幻无穷 ...