使用jsonphp解析twitter状态


parse twitter status with json php

我正在尝试解析twitter用户统计数据(使用php)——我对解析json非常不熟悉,所以这让我有点头疼(我知道这里遗漏了一些愚蠢的东西)。我看了很多其他人的回答/帖子,但都记不清了。任何帮助都很感激。

本质上,我想要得到的是[url]和[text],但我在php中不断出现错误。

[0] => stdClass Object
    (
        [in_reply_to_user_id] => 
        [in_reply_to_status_id_str] => 
        [retweet_count] => 0
        [id_str] => 180735710897250304
        [in_reply_to_user_id_str] => 
        [geo] => 
        [favorited] => 
        [user] => stdClass Object
            (
                [id_str] => 46023
                [id] => 460
            )
        [in_reply_to_status_id] => 
        [in_reply_to_screen_name] => 
        [truncated] => 
        [contributors] => 
        [source] => Facebook
        [entities] => stdClass Object
            (
                [hashtags] => Array
                    (
                    )
                [urls] => Array
                    (
                        [0] => stdClass Object
                            (
                                [expanded_url] => http://www.buzzfeed.co
                                [url] => http://t.c
                                [indices] => Array
                                    (
                                        [0] => 0
                                        [1] => 20
                                    )
                                [display_url] => buzzfeed.com/c
                            )
                        [1] => stdClass Object
                            (
                                [expanded_url] => http://fb
                                [url] => http://t.
                                [indices] => Array
                                    (
                                        [0] => 21
                                        [1] => 41
                                    )
                                [display_url] => fb.m
                            )
                    )
                [user_mentions] => Array
                    (
                    )
            )
        [coordinates] => 
        [retweeted] => 
        [id] => 180735
        [created_at] => Fri Mar 16 19:22:06 +0000 2012
        [possibly_sensitive] => 
        [place] => 
        [text] => http://t.co

json_decode函数采用可选的第二个参数来返回关联数组,而不是对象。

尝试:json_decode($your_json_var, false)

然后,您可以像使用任何数组一样正常地检索值。

您还可以获得以下值,例如:

 echo $tweets[0]->id_str;

这将显示json数据中的id_str值。