复杂的结构化JSON迭代

时间:2012-05-30 01:08:54

标签: javascript jquery json

我试图使用以下代码迭代下面的JSON对象:

$.each(response.payload.entries, function (i, data) {

            var title = data.uid;
            var dis = data.text;
            var url = data.http;
});

这是我试图迭代的JSON对象。

for (;;);
{
   "__ar":1,
   "payload":{
      "entries":[
         {
            "uid":617823537,
            "photo":"http:\/\/profile.ak.fbcdn.net\/hprofile-ak-snc4\/371505_617823537_1432230628_q.jpg",
            "type":"user",
            "text":"Nikh Il",
            "path":"\/nikh.il.351",
            "category":"Pune, Maharashtra",
            "names":[
               "Nikh Il"
            ],
            "subtext":""
         },
         {
            "uid":100001702504174,
            "photo":"http:\/\/profile.ak.fbcdn.net\/hprofile-ak-ash2\/157844_100001702504174_408730515_q.jpg",
            "type":"user",
            "text":"Nilam Patil",
            "path":"\/profile.php?id=100001702504174",
            "category":"Pune, Maharashtra",
            "names":[
               "Nilam Patil"
            ],
            "subtext":""
         },
         {
            "uid":100001485070815,
            "photo":"http:\/\/profile.ak.fbcdn.net\/hprofile-ak-snc4\/48955_100001485070815_1517928098_q.jpg",
            "type":"user",
            "text":"Nisha Lemos",
            "path":"\/profile.php?id=100001485070815",
            "category":"Pune, Maharashtra",
            "names":[
               "Nisha Lemos"
            ],
            "subtext":""
         },
         {
            "uid":100002112294701,
            "photo":"http:\/\/profile.ak.fbcdn.net\/hprofile-ak-snc4\/48784_100002112294701_37411980_q.jpg",
            "type":"user",
            "text":"Nilesh Pathare",
            "path":"\/profile.php?id=100002112294701",
            "category":"Pune, Maharashtra",
            "names":[
               "Nilesh Pathare"
            ],
            "subtext":""
         },
         {
            "uid":100003485631681,
            "photo":"http:\/\/profile.ak.fbcdn.net\/hprofile-ak-snc4\/173739_100003485631681_1898829160_q.jpg",
            "type":"user",
            "text":"Nilesh Pathare",
            "path":"\/profile.php?id=100003485631681",
            "category":"Pune, Maharashtra",
            "names":[
               "Nilesh Pathare"
            ],
            "subtext":""
         },
         {
            "uid":100002020083795,
            "photo":"http:\/\/profile.ak.fbcdn.net\/hprofile-ak-ash2\/276120_100002020083795_155025323_q.jpg",
            "type":"user",
            "text":"Nikhil Pataskar",
            "path":"\/nikpataskar",
            "category":"Pune, Maharashtra",
            "names":[
               "Nikhil Pataskar"
            ],
            "subtext":""
         },
         {
            "uid":100000619107842,
            "photo":"http:\/\/profile.ak.fbcdn.net\/hprofile-ak-snc4\/260864_100000619107842_139211244_q.jpg",
            "type":"user",
            "text":"Nikhil Agarwal",
            "path":"\/profile.php?id=100000619107842",
            "category":"Pune, Maharashtra",
            "names":[
               "Nikhil Agarwal"
            ],
            "subtext":""
         },
         {
            "uid":137827210650,
            "type":"app",
            "path":"http:\/\/apps.facebook.com\/ninjasaga\/",
            "text":"Ninja Saga",
            "dataGT":"{\"ref\":\"search_typeahead\",\"type\":\"click2canvas\"}",
            "subtext":"4,100,000 monthly users",
            "category":"Game",
            "photo":"http:\/\/photos-f.ak.fbcdn.net\/photos-ak-snc7\/v43\/150\/137827210650\/app_1_137827210650_3577.gif"
         },
         {
            "uid":102796083108857,
            "type":"app",
            "path":"http:\/\/www.facebook.com\/apps\/application.php?id=102796083108857",
            "text":"Nimbuzz Mobile",
            "subtext":"5,600,000 monthly users",
            "category":"App",
            "photo":"http:\/\/photos-g.ak.fbcdn.net\/photos-ak-snc7\/v27562\/173\/102796083108857\/app_1_102796083108857_3242.gif"
         },
         {
            "uid":84697719333,
            "type":"app",
            "path":"http:\/\/www.facebook.com\/apps\/application.php?id=84697719333",
            "text":"Nike",
            "subtext":"470,000 monthly users",
            "category":"App",
            "photo":"http:\/\/photos-a.ak.fbcdn.net\/photos-ak-snc7\/v85006\/213\/84697719333\/app_1_84697719333_5631.gif"
         }
      ]
   }
}

我在JS控制台中收到此错误

Uncaught SyntaxError: Unexpected token : 

我现在收到错误!!它的for(;;);

如何用一些空格替换它以使它仍然是一个有效的JSON对象?

1 个答案:

答案 0 :(得分:0)

对于此任务,您可以使用DefiantJS(http://defiantjs.com)。此lib使用带有XPath查询的方法“search”扩展全局对象JSON。它返回数组中的匹配项(如果未找到匹配项,则返回空数组)。

这是以下代码的工作小提琴;
http://jsfiddle.net/hbi99/KZ68m/

var data = {
       "__ar": 1,
       "payload": {
          "entries": [
            ...
          ]
       }
    },
    res = JSON.search( data, '//payload/entries' );

for (var i=0; i<res.length; i++) {
    console.log( res[i] );
}

如果您不熟悉XPath(标准化查询语言),可以试用这个XPath评估程序,让您了解它是多么容易。
http://defiantjs.com/#xpath_evaluator