在没有根数组的情况下扩展JSON?

时间:2014-02-07 19:43:37

标签: jquery json

我有一个类似的JSON Feed:

    {
      "0":{
        "created_at":"Thu Feb 06 23:44:35 +0000 2014",
        "id":431574171332531234,
        "id_str":"431574171332531234",
        "text":"This is a tweet that mentions @SomeName",
        "source":"web",
        "truncated":false,
        "in_reply_to_status_id":null,
        "in_reply_to_status_id_str":null,
        "in_reply_to_user_id":null,
        "in_reply_to_user_id_str":null,
        "in_reply_to_screen_name":null,
        "user":{
          "id":830531234,
          "id_str":"830531234",
          "name":"ThatGuy",
          "screen_name":"ThatGuy",
          "location":"Awesomeville, California",
          "description":"By @ASomeName",
          "url":"http:\/\/t.co\/Y9LdOHlolz",
          "entities":{
            "url":{
              "urls":[
                {
                  "url":"http:\/\/t.co\/Y9LdOHlolz",
                  "expanded_url":"http:\/\/thatguysite.com",
                  "display_url":"thatguysite.com",
                  "indices":[
                    0,
                    22
                  ]
                }
              ]
            },
            "description":{
              "urls":[

              ]
            }
          },
          "protected":false,
          "followers_count":5,
          "friends_count":1,
          "listed_count":0,
          "created_at":"Tue Sep 18 07:17:35 +0000 2012",
          "favourites_count":0,
          "utc_offset":-28800,
          "time_zone":"Pacific Time (US & Canada)",
          "geo_enabled":false,
          "verified":false,
          "statuses_count":1,
          "lang":"en",
          "contributors_enabled":false,
          "is_translator":false,
          "is_translation_enabled":false,
          "profile_background_color":"FFFFFF",
          "profile_background_image_url":"http:...jpeg",
          "profile_background_image_url_https":"https:...jpeg",
          "profile_background_tile":false,
          "profile_image_url":"http:...png",
          "profile_image_url_https":"https:...png",
          "profile_banner_url":"https:",
          "profile_link_color":"676767",
          "profile_sidebar_border_color":"FFFFFF",
          "profile_sidebar_fill_color":"DDEEF6",
          "profile_text_color":"333333",
          "profile_use_background_image":true,
          "default_profile":false,
          "default_profile_image":false,
          "following":false,
          "follow_request_sent":false,
          "notifications":false
        },
        "geo":null,
        "coordinates":null,
        "place":null,
        "contributors":null,
        "retweet_count":0,
        "favorite_count":0,
        "entities":{
          "hashtags":[

          ],
          "symbols":[

          ],
          "urls":[

          ],
          "user_mentions":[
            {
              "screen_name":"SomeName",
              "name":"Some Name",
              "id":57701234,
              "id_str":"57701234",
              "indices":[
                41,
                54
              ]
            }
          ]
        },
        "favorited":false,
        "retweeted":false,
        "lang":"en"
      },
      "httpstatus":200
    }

...我正在尝试使用以下方式扩展信息:

    $.getJSON('twitter/twitter.php', function( result ) {           
      var curData = result['0'];
      var tweetUN = curData.user.name;
      var tweetWho = curData.entities.user_mentions.screen_name;
      var tweetBody = curData.type;
          tweets = '<div class="tweet"><div class="un">' + tweetUN + '</div><div class="who">@' + tweetWho + '</div><div class="content">' + tweetBody + '</div></div>';
      $('#tweetwrap').append(tweets);
      // todo: limit to 4 tweets max
    });

...我希望输出类似于:

    <div class="tweet">
      <div class="un">ThatGuy</div>
      <div class="who">@SomeName</div>
      <div class="content">This is a tweet that mentions @SomeName</div>
    </div>

代码正在拉curData.user.name和curData.text,但我不知道如何显示curData.entities.user_mentions.screen_name。非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

那将是

curData.entities.user_mentions[0].screen_name;

因为user_mentions是一个数组,如果它可以包含多个值,则必须迭代

var tweetWho = $.map(curData.entities.user_mentions, function(ent) {
    return ent.screen_name;
});

然后你可以做

$('.content').text('This is a tweet that mentions @' + tweetWho.join(', @'));

答案 1 :(得分:1)

使用@adeneo的建议我能够想出这个:

    $.getJSON('URL_TO_JSON', function( result ) {          
      var curData = $.map(result, function(val, key) {
        if ( key < 4 ){ // limits the feed to 4 tweets max
          var tweetUN = val.user.name;
          var tweetWho = $.map(val.entities.user_mentions, function(ent) {
              return '<a href="https://twitter.com/' + ent.screen_name + '" target="_blank">@' + ent.screen_name + '</a>';
          });
          var tweetBody = val.text;
          var tweets = '<div class="tweet"><div class="un">' + tweetUN + '</div><div class="who">' + tweetWho.join(' - ') + '</div><div class="content">' + tweetBody + '</div></div>';
          $('#tweetwrap').append(tweets);
        }
      });
    });

$ .map函数将JSON对象转换为数组,并为该数组的每个索引分配一个函数。此外,如果你不添加连接(' - ')或类似的东西,默认是一个没有空格的逗号。