2013-08-16 156 views
0

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

如何只显示文本内的内容?

+0

如果'$ twitteruser'来自请求,在' - > get()'中使用'urlencode()'之前是明智的。 –

回答

3

使用

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

OP想要从'JSON'获取特定项目,而不是将特定项目编码为'JSON'。 –

+0

@YogeshSuthar再次读取OP代码:) api返回数组 – DevZer0

+0

@ DevZer0这有问题'任何人都可以帮我从json获取特定项目吗?'。 –

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; 

这为我工作就在昨天

0

$鸣叫= $连接 - > GET( “https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=”。$ twitteruser。 “&计数=”。 $ notweets);

返回对象的数组,所以你可以简单地前 json_encode得到它,就像

回声$鸣叫[0] - >文本;

如果你希望你的json_encode后一样,你可以做以下

代码

回声$ JSON = json_encode($鸣叫); //将你的输出放到变量中

$ arr = json_decode($ json);

echo $ arr [0] - > text;