PHP不允许我访问解码的JSON

时间:2012-03-19 07:07:48

标签: php json

我使用API​​进行JSON调用并获取以下内容,这似乎是正确格式化的JSON:

{
    "pagination": {},
    "meta": {
        "code": 200
    },
    "data": [
        {
            "tags": [],
            "location": {
                "latitude": 37.42833,
                "name": "Stanford University",
                "longitude": -122.1668,
                "id": 10138861
            },
            "comments": {
                "count": 0,
                "data": []
            },
            "filter": "Rise",
            "created_time": "1331327429",
            "link": "http://instagr.am/p/H91ykZpqUW/",
            "likes": {
                "count": 3,
                "data": [
                    {
                        "username": "razzles39",
                        "profile_picture": "http://images.instagram.com/profiles/profile_14316422_75sq_1322705511.jpg",
                        "id": "14316422",
                        "full_name": "razzles39"
                    },
                    {
                        "username": "mscaliti",
                        "profile_picture": "http://images.instagram.com/profiles/profile_10827166_75sq_1330704753.jpg",
                        "id": "10827166",
                        "full_name": "mscaliti"
                    },
                    {
                        "username": "mariecox",
                        "profile_picture": "http://images.instagram.com/profiles/profile_3987147_75sq_1324863102.jpg",
                        "id": "3987147",
                        "full_name": "Marie Cox"
                    }
                ]
            },
            "images": {
                "low_resolution": {
                    "url": "http://distilleryimage6.s3.amazonaws.com/4c6f97c46a2c11e180c9123138016265_6.jpg",
                    "width": 306,
                    "height": 306
                },
                "thumbnail": {
                    "url": "http://distilleryimage6.s3.amazonaws.com/4c6f97c46a2c11e180c9123138016265_5.jpg",
                    "width": 150,
                    "height": 150
                },
                "standard_resolution": {
                    "url": "http://distilleryimage6.s3.amazonaws.com/4c6f97c46a2c11e180c9123138016265_7.jpg",
                    "width": 612,
                    "height": 612
                }
            },
            "caption": {
                "created_time": "1331327500",
                "text": "Chillin with Brady at Stanford",
                "from": {
                    "username": "nicolelainefox",
                    "profile_picture": "http://images.instagram.com/profiles/anonymousUser.jpg",
                    "id": "17982472",
                    "full_name": "nicolelainefox"
                },
                "id": "143507936292283648"
            },
            "type": "image",
            "id": "143507334669706518_17982472",
            "user": {
                "username": "nicolelainefox",
                "website": "",
                "bio": "",
                "profile_picture": "http://images.instagram.com/profiles/anonymousUser.jpg",
                "full_name": "nicolelainefox",
                "id": "17982472"
            }
        }
    ]
}

然后我在我的PHP脚本中使用json_decode来实现我可以操作的东西,这是var_dump的输出

object(stdClass)#1 (3) { ["pagination"]=> object(stdClass)#2 (0) { } ["meta"]=> object(stdClass)#3 (1) { ["code"]=> int(200) } ["data"]=> array(1) { [0]=> object(stdClass)#4 (12) { ["tags"]=> array(0) { } ["location"]=> object(stdClass)#5 (4) { ["latitude"]=> float(37.42833) ["name"]=> string(19) "Stanford University" ["longitude"]=> float(-122.1668) ["id"]=> int(10138861) } ["comments"]=> object(stdClass)#6 (2) { ["count"]=> int(0) ["data"]=> array(0) { } } ["filter"]=> string(4) "Rise" ["created_time"]=> string(10) "1331327429" ["link"]=> string(31) "http://instagr.am/p/H91ykZpqUW/" ["likes"]=> object(stdClass)#7 (2) { ["count"]=> int(3) ["data"]=> array(3) { [0]=> object(stdClass)#8 (4) { ["username"]=> string(9) "razzles39" ["profile_picture"]=> string(73) "http://images.instagram.com/profiles/profile_14316422_75sq_1322705511.jpg" ["id"]=> string(8) "14316422" ["full_name"]=> string(9) "razzles39" } [1]=> object(stdClass)#9 (4) { ["username"]=> string(8) "mscaliti" ["profile_picture"]=> string(73) "http://images.instagram.com/profiles/profile_10827166_75sq_1330704753.jpg" ["id"]=> string(8) "10827166" ["full_name"]=> string(8) "mscaliti" } [2]=> object(stdClass)#10 (4) { ["username"]=> string(8) "mariecox" ["profile_picture"]=> string(72) "http://images.instagram.com/profiles/profile_3987147_75sq_1324863102.jpg" ["id"]=> string(7) "3987147" ["full_name"]=> string(9) "Marie Cox" } } } ["images"]=> object(stdClass)#11 (3) { ["low_resolution"]=> object(stdClass)#12 (3) { ["url"]=> string(79) "http://distilleryimage6.s3.amazonaws.com/4c6f97c46a2c11e180c9123138016265_6.jpg" ["width"]=> int(306) ["height"]=> int(306) } ["thumbnail"]=> object(stdClass)#13 (3) { ["url"]=> string(79) "http://distilleryimage6.s3.amazonaws.com/4c6f97c46a2c11e180c9123138016265_5.jpg" ["width"]=> int(150) ["height"]=> int(150) } ["standard_resolution"]=> object(stdClass)#14 (3) { ["url"]=> string(79) "http://distilleryimage6.s3.amazonaws.com/4c6f97c46a2c11e180c9123138016265_7.jpg" ["width"]=> int(612) ["height"]=> int(612) } } ["caption"]=> object(stdClass)#15 (4) { ["created_time"]=> string(10) "1331327500" ["text"]=> string(30) "Chillin with Brady at Stanford" ["from"]=> object(stdClass)#16 (4) { ["username"]=> string(14) "nicolelainefox" ["profile_picture"]=> string(54) "http://images.instagram.com/profiles/anonymousUser.jpg" ["id"]=> string(8) "17982472" ["full_name"]=> string(14) "nicolelainefox" } ["id"]=> string(18) "143507936292283648" } ["type"]=> string(5) "image" ["id"]=> string(27) "143507334669706518_17982472" ["user"]=> object(stdClass)#17 (6) { ["username"]=> string(14) "nicolelainefox" ["website"]=> string(0) "" ["bio"]=> string(0) "" ["profile_picture"]=> string(54) "http://images.instagram.com/profiles/anonymousUser.jpg" ["full_name"]=> string(14) "nicolelainefox" ["id"]=> string(8) "17982472" } } } }

然而,当我使用echo($instagram_data["data"]);时,一切都会崩溃。如何访问此关联数组中的“数据”数组?这是完整的代码:

$instagram_handler = fopen("https://api.instagram.com/v1/locations/10138861/media/recent/?client_id=MY_ID", "r");
$instagram_json = stream_get_contents($instagram_handler);
fclose($instagram_handler);                                                                                                                                                      
$instagram_data = json_decode($instagram_json);                                                                                                                                                 
echo($instagram_data["data"]); //Breaks page

2 个答案:

答案 0 :(得分:1)

JSON被读取为对象,而不是关联数组。加载它就像这样,你会很好:

$instagram_data = json_decode($instagram_json, TRUE);

或者,像你现在一样加载它,运行:

echo $instagram_data->data

答案 1 :(得分:0)

请注意json_decode函数的assoc参数。如果省略它,则解码将JSON视为对象而不是数组。所以你应该能够通过以下方式访问它:

$instagram_data->data;

其他选项是按以下方式解码为数组:

$instagram_data = json_decode($instagram_json, TRUE);