Php 如何从这个json结构中获取值?
我有访问JSON的PHP代码:Php 如何从这个json结构中获取值?,php,json,Php,Json,我有访问JSON的PHP代码: $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) { forea
$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"}}]}
第二个文件实际上包含两个单独的JSON数组 您需要为其中的每一个添加一个数组键,以便它可以将文件的整个内容解析为单个JSON数组 更改此项:
{
"pagination": {
},
"meta": {
"code": 200
}
etc.....
etc.....
为此:
{
0 : {
"pagination": {
},
"meta": {
"code": 200
},
etc....
etc....
1 : {
"pagination": {
},
"meta": {
"code": 200
},
etc....
etc....
}
然后可以将整个缓存解析为单个JSON数组。
像这样:
第二个JSON片段不是有效的JSON。尝试先使用或类似的方法验证JSON。我想是的。Instagram API返回第一个JSON。有分页属性可以访问下一张照片并返回下一个JSON。因此,我尝试将所有json返回合并到一个名为cache.json的平面文件中,其结构类似于第二个json片段。第二个片段有两个json对象背靠背,就像您将两个json对象串接在一起一样。您可以将这些对象组合成一个数组。类似于[obj1,obj2,obj3]的东西。抱歉,如果我误解了这个问题。您无法通过简单地将它们附加在一起来创建有效的JSON对象表示。您的JSON需要如下所示-我更新了问题,使其验证为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....
}