【问题标题】:Trouble with $.getJSON - looks like arrays inside of arrays$.getJSON 的问题 - 看起来像数组中的数组
【发布时间】:2011-02-10 04:20:02
【问题描述】:

好的,我对 jQuery JSON 的东西还很陌生。我正在尝试访问以下 json 输出中的 actions > name 节点。看起来“动作”是“数据”数组中的一个数组。我获取 json 结果的方式可以使用fb.message 正确访问“消息”部分,并且可以使用fb.from.name 访问“来自”下的“名称”节点。那么如何访问“actions”array下的“name”节点呢?

感谢您的帮助!!

代码:

({ "data": [{
         "id": "1755670903_1287007912714",
         "from": {
            "name": "LifeBridge Church",
            "id": "1755670903"
         },
         "message": "Due to weather, church service tomorrow is canceled. See you next week!",
         "icon": "http://photos-d.ak.fbcdn.net/photos-ak-snc1/v27562/23/2231777543/app_2_2231777543_9553.gif",
         "actions": [
            {
               "name": "\u0040lifebridgeTX on Twitter",
               "link": "http://twitter.com/lifebridgeTX?utm_source=fb&utm_medium=fb&utm_campaign=lifebridgeTX&utm_content=33711605665505280"
            }
         ],
         "type": "status",
         "application": {
            "name": "Twitter",
            "id": "2231777543"
         },
         "created_time": "2011-02-05T02:20:48+0000",
         "updated_time": "2011-02-05T02:20:48+0000"
      },
      {
         "id": "1755670903_1281724020620",
         "from": {
            "name": "LifeBridge Church",
            "id": "1755670903"
         },
         "message": "Service tonight at 5:30... meet us at Eddins Elementary school in McKinney.  http://see.sc/8l4Cwo || Praying that God is greatly glorified!",
         "icon": "http://photos-d.ak.fbcdn.net/photos-ak-snc1/v27562/23/2231777543/app_2_2231777543_9553.gif",
         "actions": [
            {
               "name": "\u0040lifebridgeTX on Twitter",
               "link": "http://twitter.com/lifebridgeTX?utm_source=fb&utm_medium=fb&utm_campaign=lifebridgeTX&utm_content=31388610301272065"
            }
         ],
         "type": "status",
         "application": {
            "name": "Twitter",
            "id": "2231777543"
         },
         "created_time": "2011-01-29T16:30:03+0000",
         "updated_time": "2011-01-29T16:30:03+0000"
      }]
});

其他问题的后续代码:

$.each(json.data,function(i,fb){
    if (!fb.message) continue;
    facebookPost += '<li class="ui-li ui-li-static ui-btn-up-c" role="option" data-theme="c">' + fb.message + ' <br /><span class="via">' + fb.created_at + ' via ' + fb.actions[0].name + '</span></li>';
    $('#facebookPosts li:first').after(facebookPost);
});

【问题讨论】:

    标签: jquery arrays json getjson


    【解决方案1】:

    当你说fb 时,我假设fb = data[0]。那么:

    fb.actions; // Gives actions ARRAY
    fb.actions[0]; // Gives first OBJECT in actions ARRAY
    fb.actions[0].name; // Gives the name VALUE of the first OBJECT in actions ARRAY
    

    从 cmets 中的一个问题,当值不存在时跳过项目:

    $.each(json.data,function(i,fb){
        if (!fb.message) return true; // true to keep going, false to quit immediately
        facebookPost += '<li class="ui-li ui-li-static ui-btn-up-c" role="option" data-theme="c">' + fb.message + ' <br /><span class="via">' + fb.created_at + ' via ' + fb.actions[0].name + '</span></li>';
        $('#facebookPosts li:first').after(facebookPost);
    });
    

    【讨论】:

    • 谢谢,这正是我所需要的!另外,如果您碰巧知道...我正在遍历数据数组中的所有对象。如果返回未定义,我如何从循环中删除一个?或者有没有简单的方法来做到这一点?如果您需要代码示例,请告诉我。
    • @Ryan,什么时候返回undefined?删除,你的意思是跳过它(跳到循环中的下一个项目)?
    • 抱歉,这有点含糊。是的,有些对象没有“消息”,我的循环创建了一个列表项,如下所示:(这是我的 $.each 中的代码)fbPost += '&lt;li class="ui-li ui-li-static ui-btn-up-c" role="option" data-theme="c"&gt;' + fb.message + '&lt;br /&gt;&lt;span class="via"&gt;via ' + fb.actions[0].name + '&lt;/span&gt;&lt;/li&gt;';。如果没有“消息”,我希望循环跳过它。
    • @Ryan,在我的回答中添加了回复。
    • @Box9,我现在收到一个语法错误:“for (...) {\n”。我拿了你的代码,只是在里面添加了上面的fbPost += '...';。我需要对你的“for”中的“...”做任何事情吗?
    【解决方案2】:

    JSON 对象只是普通的 javascript 对象,以与通常相同的方式访问它们,要获取 ith 数据项的 jth 操作,您可以使用以下代码。要获得第一个,您只需硬编码i=j=0

    jsonResponse.data[i].actions[j].name
    

    【讨论】:

    • 谢谢...您和其他回复帮助解决了我的问题!我选择和他一起去,但我仍然会给你一个观点。
    【解决方案3】:

    你可以访问数组中的第一个元素:

    fb.actions[0].name
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 2020-05-31
      • 2017-08-23
      • 2011-04-02
      • 1970-01-01
      • 2012-03-21
      • 2023-01-20
      相关资源
      最近更新 更多