简体   繁体   中英

getting value from nested json response

Here is my json response where I want to fetch the User Id value.

I saw similar threads but did not help me.

Here is code how I acheive:

$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?
        }

And Json response:

[
  {
    "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
  },
  {

UPDATED CODE

$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 />";
        }

But this does not give any output.

You can do

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

$data = json_decode($tweets3,true);

Now $data will be an array and you can loop through and get the values

UPDATE ON DISPLAYING ITEMS

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

}

In the above I just displayed tweet text and user name, you can print_r($data) before the loop and see the different element names and can display within the loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM