如何从这个json结构中获取价值?

时间:2014-01-11 03:13:01

标签: php json

我有PHP代码来访问JSON:

$cache = './cache.json'; 
$response = json_decode(file_get_contents($cache),true); //Decode as an json array 

if(is_array($response['data'])){ 
foreach ($response['data'] as $data) { 

if ($data['comments']['data'] != null) {  
foreach($data['comments']['data'] as $comment){ 

...

上面的代码适用于此JSON结构:

{
"pagination": {

},
"meta": {
    "code": 200
},
"data": [
    {
        "attribution": null,
        "tags": [

        ],
        "type": "image",
        "location": null,
        "comments": {
            "count": 0,
            "data": [

            ]
        },
        "filter": "Normal",
        "created_time": "1339298544",
        "link": "http:\/\/instagram.com\/p\/LrZfG6sU_l\/",
        "likes": {
            "count": 0,
            "data": [

            ]
        },
        "images": {
            "low_resolution": {
                "url": "http:\/\/distilleryimage5.s3.amazonaws.com\/7f69259cb2ab11e1989612313815112c_6.jpg",
                "width": 306,
                "height": 306
            },
            "thumbnail": {
                "url": "http:\/\/distilleryimage5.s3.amazonaws.com\/7f69259cb2ab11e1989612313815112c_5.jpg",
                "width": 150,
                "height": 150
            },
            "standard_resolution": {
                "url": "http:\/\/distilleryimage5.s3.amazonaws.com\/7f69259cb2ab11e1989612313815112c_7.jpg",
                "width": 612,
                "height": 612
            }
        },
        "users_in_photo": [

        ],
        "caption": null,
        "user_has_liked": false,
        "id": "210373896496566245_180213154",
        "user": {
            "username": "realwahyuputra",
            "website": "",
            "profile_picture": "http:\/\/images.ak.instagram.com\/profiles\/profile_180213154_75sq_1359089013.jpg",
            "full_name": "realwahyuputra",
            "bio": "",
            "id": "180213154"
        }
    }
]
}

但是当访问JSON结构时它不起作用:

{
"pagination": {

},
"meta": {
    "code": 200
},
"data": [
    {
        "attribution": null,
        "tags": [

        ],
        "type": "image",
        "location": null,
        "comments": {
            "count": 0,
            "data": [

            ]
        },
        "filter": "Normal",
        "created_time": "1339298544",
        "link": "http:\/\/instagram.com\/p\/LrZfG6sU_l\/",
        "likes": {
            "count": 0,
            "data": [

            ]
        },
        "images": {
            "low_resolution": {
                "url": "http:\/\/distilleryimage5.s3.amazonaws.com\/7f69259cb2ab11e1989612313815112c_6.jpg",
                "width": 306,
                "height": 306
            },
            "thumbnail": {
                "url": "http:\/\/distilleryimage5.s3.amazonaws.com\/7f69259cb2ab11e1989612313815112c_5.jpg",
                "width": 150,
                "height": 150
            },
            "standard_resolution": {
                "url": "http:\/\/distilleryimage5.s3.amazonaws.com\/7f69259cb2ab11e1989612313815112c_7.jpg",
                "width": 612,
                "height": 612
            }
        },
        "users_in_photo": [

        ],
        "caption": null,
        "user_has_liked": false,
        "id": "210373896496566245_180213154",
        "user": {
            "username": "realwahyuputra",
            "website": "",
            "profile_picture": "http:\/\/images.ak.instagram.com\/profiles\/profile_180213154_75sq_1359089013.jpg",
            "full_name": "realwahyuputra",
            "bio": "",
            "id": "180213154"
        }
    }
]
}
{
"pagination": {

},
"meta": {
    "code": 200
},
"data": [
    {
        "attribution": null,
        "tags": [

        ],
        "type": "image",
        "location": null,
        "comments": {
            "count": 0,
            "data": [

            ]
        },
        "filter": "Normal",
        "created_time": "1339298544",
        "link": "http:\/\/instagram.com\/p\/LrZfG6sU_l\/",
        "likes": {
            "count": 0,
            "data": [

            ]
        },
        "images": {
            "low_resolution": {
                "url": "http:\/\/distilleryimage5.s3.amazonaws.com\/7f69259cb2ab11e1989612313815112c_6.jpg",
                "width": 306,
                "height": 306
            },
            "thumbnail": {
                "url": "http:\/\/distilleryimage5.s3.amazonaws.com\/7f69259cb2ab11e1989612313815112c_5.jpg",
                "width": 150,
                "height": 150
            },
            "standard_resolution": {
                "url": "http:\/\/distilleryimage5.s3.amazonaws.com\/7f69259cb2ab11e1989612313815112c_7.jpg",
                "width": 612,
                "height": 612
            }
        },
        "users_in_photo": [

        ],
        "caption": null,
        "user_has_liked": false,
        "id": "210373896496566245_180213154",
        "user": {
            "username": "realwahyuputra",
            "website": "",
            "profile_picture": "http:\/\/images.ak.instagram.com\/profiles\/profile_180213154_75sq_1359089013.jpg",
            "full_name": "realwahyuputra",
            "bio": "",
            "id": "180213154"
        }
    }
]
}
{
"pagination": {

},
"meta": {
    "code": 200
},
"data": [
    {
        "attribution": null,
        "tags": [

        ],
        "type": "image",
        "location": null,
        "comments": {
            "count": 0,
            "data": [

            ]
        },
        "filter": "Normal",
        "created_time": "1339298544",
        "link": "http:\/\/instagram.com\/p\/LrZfG6sU_l\/",
        "likes": {
            "count": 0,
            "data": [

            ]
        },
        "images": {
            "low_resolution": {
                "url": "http:\/\/distilleryimage5.s3.amazonaws.com\/7f69259cb2ab11e1989612313815112c_6.jpg",
                "width": 306,
                "height": 306
            },
            "thumbnail": {
                "url": "http:\/\/distilleryimage5.s3.amazonaws.com\/7f69259cb2ab11e1989612313815112c_5.jpg",
                "width": 150,
                "height": 150
            },
            "standard_resolution": {
                "url": "http:\/\/distilleryimage5.s3.amazonaws.com\/7f69259cb2ab11e1989612313815112c_7.jpg",
                "width": 612,
                "height": 612
            }
        },
        "users_in_photo": [

        ],
        "caption": null,
        "user_has_liked": false,
        "id": "210373896496566245_180213154",
        "user": {
            "username": "realwahyuputra",
            "website": "",
            "profile_picture": "http:\/\/images.ak.instagram.com\/profiles\/profile_180213154_75sq_1359089013.jpg",
            "full_name": "realwahyuputra",
            "bio": "",
            "id": "180213154"
        }
    }
]
}

如何修改我的PHP代码,以便我可以访问该JSON?

- 编辑 -

修改问题以使其验证为JSON。看下面的代码:         //收集所有Instagram API,包括下一个分页值

    <?php 
    include('conf.php'); 
    require 'instagram.class.php'; 

    $cache = './cache_coba.json'; 

    $instagram = new Instagram($accessToken); 
    $instagram->setAccessToken($accessToken); 
    $response = $instagram->getUserMedia($userID); 

    // How to Add {"data":[ at the first file

    while ($response = $instagram->pagination($response)) {
    if($response){ 
    $opn = file_get_contents($cache); 
    $opn .= json_encode($response);
    file_put_contents($cache, $opn);
    } 
    }

    // How to Add ]} at the end of file

    echo 'finish';

    ?>

    // Below is the API Response code, how to delete {"pagination" to "data":[ at the first
    // And at the end how to delete ]} and replace to comma ,

    {"pagination":{"next_url":"https:\/\/api.instagram.com\/v1\/users\/180213154\/media\/recent?access_token=a44866ef0be\u0026count=1\u0026max_id=454315833979653858_180213154","next_max_id":"454315833979653858_180213154"},"meta":{"code":200},"data":[{"attribution":null,"tags":["nikah","hadits","islam"],"type":"image","location":null,"comments":{"count":0,"data":[]},"filter":"Normal","created_time":"1368378689","link":"http:\/\/instagram.com\/p\/ZODdgMMU7i\/","likes":{"count":4,"data":[{"username":"elvina_melati","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_173812552_75sq_1388751668.jpg","id":"173812552","full_name":"Elvina melati"},{"username":"kennethtan","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_1074913_75sq_1387989988.jpg","id":"1074913","full_name":"Kenneth Tan"},{"username":"yogiealmuchtar","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_359473314_75sq_1366691245.jpg","id":"359473314","full_name":"Yogie Al Muchtar"},{"username":"dittanggraenii","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_341887668_75sq_1384157369.jpg","id":"341887668","full_name":"\u2606\u2022Ditta AnggRaeni\u2022\u2606"}]},"images":{"low_resolution":{"url":"http:\/\/distilleryimage6.s3.amazonaws.com\/fc907c08bb2611e298dd22000a9f3c77_6.jpg","width":306,"height":306},"thumbnail":{"url":"http:\/\/distilleryimage6.s3.amazonaws.com\/fc907c08bb2611e298dd22000a9f3c77_5.jpg","width":150,"height":150},"standard_resolution":{"url":"http:\/\/distilleryimage6.s3.amazonaws.com\/fc907c08bb2611e298dd22000a9f3c77_7.jpg","width":612,"height":612}},"users_in_photo":[],"caption":{"created_time":"1368378700","text":"Teruntuk calon mertuaku nun jauh disana #hadits #islam #nikah","from":{"username":"realwahyuputra","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_180213154_75sq_1359089013.jpg","id":"180213154","full_name":"realwahyuputra"},"id":"454315929551064667"},"user_has_liked":false,"id":"454315833979653858_180213154","user":{"username":"realwahyuputra","website":"","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_180213154_75sq_1359089013.jpg","full_name":"realwahyuputra","bio":"","id":"180213154"}}]}

1 个答案:

答案 0 :(得分:0)

您的第二个文件实际上包含2个单独的JSON数组。

您需要为每个数组添加一个数组键,以便它可以将整个文件内容解析为单个JSON数组。

改变这个:

{
"pagination": {

},
"meta": {
    "code": 200
} 
etc.....
etc.....

进入这个:

{
0 : {
    "pagination": {

    },
    "meta": {
        "code": 200
    },
    etc....
    etc....
1 : {
   "pagination": {

    },
    "meta": {
        "code": 200
    },
    etc....
    etc....
}

然后,您可以将整个缓存解析为单个JSON数组。 像这样:

$cached_results = json_decode(file_get_contents($cache),true); //Decode as an json array 
foreach($cached_results as $response) {
    // continue with your normal code
    if(is_array($response['data'])){ //etc....
}