繁体   English   中英

如何从JSON获取特定值?

[英]How can I get a specific value from JSON?

谁能帮我从json获取特定项目?

这是我的代码

<?php
    require_once("../twitter/twitteroauth.php"); //Path to twitteroauth library
    $twitteruser = "SokSovat";
    $notweets = 1;
    $consumerkey = "XXXXX";
    $consumersecret = "XXXXXX";
    $accesstoken = "XXXXXXX";
    $accesstokensecret = "XXXXXXX";

    function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) {
        $connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
        return $connection;
    }

    $connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);

    $tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets);

    echo json_encode($tweets);

?>

输出量

[
   {
      "created_at":"Thu Aug 15 06:20:15 +0000 2013",
      "id":3.6789347939897e+17,
      "id_str":"367893479398965248",
      "text":"\u3050\u308b\u306a\u3073\u98df\u5e02\u5834\u3067\u7523\u5730\u76f4\u9001\u30fb\u7279\u7523\u54c1\u3001\u53b3\u9078\u30bb\u30ec\u30af\u30c8\u3057\u305f\u30b0\u30eb\u30e1\u3092\u304a\u53d6\u308a\u5bc4\u305b\uff01 http:\/\/t.co\/IQRCSt2sFC\n[PR]",
      "source":"Tweet Button<\/a>",
      "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":963434528,
         "id_str":"963434528",
         "name":"Sok Sovat",
         "screen_name":"SokSovat",
         "location":"Cambodia",
         "description":"It is better to have a piece of bread with a happy heart than to have a lot of money with sadness..!!",
         "url":"http:\/\/t.co\/2QnB2KpT",
         "entities":{
            "url":{
               "urls":[
                  {
                     "url":"http:\/\/t.co\/2QnB2KpT",
                     "expanded_url":"http:\/\/soksovat.net63.net",
                     "display_url":"soksovat.net63.net",
                     "indices":[
                        0,
                        20
                     ]
                  }
               ]
            },
            "description":{
               "urls":[

               ]
            }
         },
         "protected":false,
         "followers_count":10,
         "friends_count":26,
         "listed_count":0,
         "created_at":"Thu Nov 22 01:47:04 +0000 2012",
         "favourites_count":0,
         "utc_offset":null,
         "time_zone":null,
         "geo_enabled":false,
         "verified":false,
         "statuses_count":174,
         "lang":"en",
         "contributors_enabled":false,
         "is_translator":false,
         "profile_background_color":"C0DEED",
         "profile_background_image_url":"http:\/\/a0.twimg.com\/images\/themes\/theme1\/bg.png",
         "profile_background_image_url_https":"https:\/\/si0.twimg.com\/images\/themes\/theme1\/bg.png",
         "profile_background_tile":false,
         "profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/2879880469\/060f9351577d0ab019fa5b1036210931_normal.jpeg",
         "profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/2879880469\/060f9351577d0ab019fa5b1036210931_normal.jpeg",
         "profile_link_color":"0084B4",
         "profile_sidebar_border_color":"C0DEED",
         "profile_sidebar_fill_color":"DDEEF6",
         "profile_text_color":"333333",
         "profile_use_background_image":true,
         "default_profile":true,
         "default_profile_image":false,
         "following":null,
         "follow_request_sent":false,
         "notifications":null
      },
      "geo":null,
      "coordinates":null,
      "place":null,
      "contributors":null,
      "retweet_count":0,
      "favorite_count":0,
      "entities":{
         "hashtags":[

         ],
         "symbols":[

         ],
         "urls":[
            {
               "url":"http:\/\/t.co\/IQRCSt2sFC",
               "expanded_url":"http:\/\/bit.ly\/150qrue",
               "display_url":"bit.ly\/150qrue",
               "indices":[
                  36,
                  58
               ]
            }
         ],
         "user_mentions":[

         ]
      },
      "favorited":false,
      "retweeted":false,
      "possibly_sensitive":false,
      "lang":"ja"
   }
]

我想要的是

实际上,我只想

"text":"\u3050\u308b\u306a\u3073\u98df\u5e02\u5834\u3067\u7523\u5730\u76f4\u9001\u30fb\u7279\u7523\u54c1\u3001\u53b3\u9078\u30bb\u30ec\u30af\u30c8\u3057\u305f\u30b0\u30eb\u30e1\u3092\u304a\u53d6\u308a\u5bc4\u305b\uff01 http:\/\/t.co\/IQRCSt2sFC\n[PR]"

如何只显示文本内容?

采用

echo json_encode($tweets[0]['text']);
// decode json result to access each key in the array
$la_data = json_decode($tweets);

$lo_tweet = reset($la_data);

if (!is_object($lo_tweet))
    exit;

echo $lo_tweet->text;

昨天才对我有用

$ tweets = $ connection-> get(“ https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=”。$ twitteruser。“&count =”。$ notweets);

返回一个object数组,因此您可以简单地在json_encode之前获取它,例如

echo $ tweets [0]-> text;

如果想在json_encode之后输入相同的代码,则可以执行以下操作

您的代码

echo $ json = json_encode($ tweets); //将输出放到变量中

$ arr = json_decode($ json);

echo $ arr [0]-> text;

暂无
暂无

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

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