繁体   English   中英

从嵌套的json响应中获取价值

[英]getting value from nested json response

这是我要获取用户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?
        }

杰森回应:

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

但这没有任何输出。

你可以做

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

}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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