如何从JSON获取特定值?

时间:2013-08-16 04:02:06

标签: php json twitter

有人可以帮助我从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]"

如何只显示文字内容?

3 个答案:

答案 0 :(得分:3)

使用

echo json_encode($tweets[0]['text']);

答案 1 :(得分:0)

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

昨天这对我有用

答案 2 :(得分:0)

  

$ tweets =   $连接 - &GT;获得( “https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=” $ twitteruser “&安培;计数=”。$ notweets);

     

返回一个对象数组,因此您可以先获取它   json_encode,如

     

echo $ tweets [0] - &gt; text;

如果您想在json_encode之后使用相同的内容,则可以执行以下操作

你的代码

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

$ ARR = json_decode($ JSON);

echo $ arr [0] - &gt; text;