我正在使用php / Laravel,并且收到API的响应,该API在控制器中返回以下格式:
// testing ...
Route::get('/test', function (){
File::delete('C:\Users\Me\Desktop\final\public\upload\users\1\5bc722d2b7c05.jpg');
});
我相信这是数组中的一个数组,是我要从中获取数据的json对象,例如,我想要记录id。 我没有运气使用这些解决方案:
[
{
"id": "474",
"room_id": "14",
"user_id": "20",
"name": "121001.webm",
"fname": "",
"status": "0",
"date_recorded": "October 17 2018 07:18:51",
"size": "396135",
"is_public": "0",
"allow_download": "0",
"privatekey": "",
"duration": "0",
"record_path": "https:example/url/test.mp4",
"record_url": "https:example/url/test.mp4"
}
]
还尝试对$response->record_url;
$response[0]->record_url;
任何帮助将不胜感激
答案 0 :(得分:5)
在JSON字符串中,您拥有and数组,其中一个元素是一个对象。
现在,根据您对它的解码方式,您将获得PHP和带有stdClass
对象的数组,或内部带有关联数组的数组。
//this will return array with stdClass object
$data = json_decode($json);
echo $data[0]->record_url;
//this will return array with associative array
$data = json_decode($json, true);
echo $data[0]['record_url'];
答案 1 :(得分:1)
尝试此代码
var_dump(json_decode($response)->record_url);
答案 2 :(得分:0)
请参考以下程序及其相应输出:
$json = '[
{
"id": "474",
"room_id": "14",
"user_id": "20",
"name": "121001.webm",
"fname": "",
"status": "0",
"date_recorded": "October 17 2018 07:18:51",
"size": "396135",
"is_public": "0",
"allow_download": "0",
"privatekey": "",
"duration": "0",
"record_path": "https:example/url/test.mp4",
"record_url": "https:example/url/test.mp4"
}
]';
$array1 = json_decode($json, false);
echo $array1[0]->id //This will print the value of id which is 474.
$array2 = json_decode($json, true);
echo $array2[0]['id'] // This will also print th evalue of id which is 474.
当TRUE时,函数json_decode的第二个参数为boolean,返回的对象将转换为关联数组。
谢谢