从嵌套的json响应中获取值

时间:2014-03-12 18:05:11

标签: php json

这是我想要获取用户ID值的json响应。

我看到类似的帖子,但没有帮助我。

以下是我如何实现的代码:

$tweets3 = $connection->get("https://api.twitter.com/1.1/statuses/home_timeline.json?include_entities=true&trim_user=".$userid."");
        foreach ($tweets3 as $item)
        {
                 echo $item->text;
                echo "<br/>";
                $tweet = $item->text;
                $userid = $item->user; // How to get user id here?
        }

和Json回应:

[
  {
    "coordinates": null,
    "truncated": false,
    "created_at": "Tue Aug 28 21:16:23 +0000 2012",
    "favorited": false,
    "id_str": "240558470661799936",
    "in_reply_to_user_id_str": null,
    "entities": {
      "urls": [
      ],
      "hashtags": [
      ],
      "user_mentions": [
      ]
    },
    "text": "just another test",
    "contributors": null,
    "id": 240558470661799936,
    "retweet_count": 0,
    "in_reply_to_status_id_str": null,
    "geo": null,
    "retweeted": false,
    "in_reply_to_user_id": null,
    "place": null,
    "source": "<a href=\"http://realitytechnicians.com\" rel=\"nofollow\">OAuth Dancer Reborn</a>",
    "user": {
      "name": "OAuth Dancer",
      "profile_sidebar_fill_color": "DDEEF6",
      "profile_background_tile": true,
      "profile_sidebar_border_color": "C0DEED",
      "profile_image_url": "http://a0.twimg.com/profile_images/730275945/oauth-dancer_normal.jpg",
      "created_at": "Wed Mar 03 19:37:35 +0000 2010",
      "location": "San Francisco, CA",
      "follow_request_sent": false,
      "id_str": "119476949",
      "is_translator": false,
      "profile_link_color": "0084B4",
      "entities": {
        "url": {
          "urls": [
            {
              "expanded_url": null,
              "url": "http://bit.ly/oauth-dancer",
              "indices": [
                0,
                26
              ],
              "display_url": null
            }
          ]
        },
        "description": null
      },
      "default_profile": false,
      "url": "http://bit.ly/oauth-dancer",
      "contributors_enabled": false,
      "favourites_count": 7,
      "utc_offset": null,
      "profile_image_url_https": "https://si0.twimg.com/profile_images/730275945/oauth-dancer_normal.jpg",
      "id": 119476949,
      "listed_count": 1,
      "profile_use_background_image": true,
      "profile_text_color": "333333",
      "followers_count": 28,
      "lang": "en",
      "protected": false,
      "geo_enabled": true,
      "notifications": false,
      "description": "",
      "profile_background_color": "C0DEED",
      "verified": false,
      "time_zone": null,
      "profile_background_image_url_https": "https://si0.twimg.com/profile_background_images/80151733/oauth-dance.png",
      "statuses_count": 166,
      "profile_background_image_url": "http://a0.twimg.com/profile_background_images/80151733/oauth-dance.png",
      "default_profile_image": false,
      "friends_count": 14,
      "following": false,
      "show_all_inline_media": false,
      "screen_name": "oauth_dancer"
    },
    "in_reply_to_screen_name": null,
    "in_reply_to_status_id": null
  },
  {

更新代码

$tweets3 = $connection->get("https://api.twitter.com/1.1/statuses/home_timeline.json?include_entities=true&trim_user=".$userid."");
        $data = json_decode($tweets3,true);
         print_r($data);
        foreach($data as $key=>$val){
                 echo "--------------------------------<br />";
                 echo "Tweet : ".$val["text"];
                 echo "<br />";
                 echo "User : ".$val["user"]["name"];
                 echo "<br />--------------------------------<br />";
        }

但这并没有给出任何结果。

1 个答案:

答案 0 :(得分:0)

你可以做到

$tweets3 = $connection->get("https://api.twitter.com/1.1/statuses/home_timeline.json?include_entities=true&trim_user=".$userid."");

$data = json_decode($tweets3,true);

现在$data将是一个数组,您可以循环并获取值

显示项目的最新信息

foreach($data as $key=>$val){
 echo "--------------------------------<br />";
 echo "Tweet : ".$val["text"];
 echo "<br />";
 echo "User : ".$val["user"]["name"];
 echo "<br />--------------------------------<br />";

}

在上面我只显示了推文文本和用户名,你可以在循环之前print_r($data)看到不同的元素名称,并且可以在循环中显示。