PHP-访问json响应中的值
我需要访问PHP中API响应返回的对象内部的值。 API响应PHP-访问json响应中的值,php,json,Php,Json,我需要访问PHP中API响应返回的对象内部的值。 API响应 $res = { "data": { "first_name": "Dany", "last_name": "mate", "id": "1379933133290837510", "image": { "60x60": { "url": "https://s-media-cache-ak0.pinimg.c
$res = {
"data": {
"first_name": "Dany",
"last_name": "mate",
"id": "1379933133290837510",
"image": {
"60x60": {
"url": "https://s-media-cache-ak0.pinimg.com/avatars/dtest_1438666564_60.jpg",
"width": 60,
"height": 60
}
}
}
}
如何访问参数“first_name”和“url”?非常感谢你的帮助。我尝试将响应转换为数组,但没有成功
$array = get_object_vars($res);
我不知道这样做正确吗
$res = json_decode('{
"data": {
"first_name": "Dany",
"last_name": "mate",
"id": "1379933133290837510",
"image": {
"60x60": {
"url": "https://s-media-cache-ak0.pinimg.com/avatars/dtest_1438666564_60.jpg",
"width": 60,
"height": 60
}
}
}
}');
$array = get_object_vars($res);
print_r($array);
您需要使用
json\u decode
进行解码。您必须首先进行json解码:
$json = json_decode($res,true);
foreach ($json['data'] as $data)
{
echo $data['first_name'];
};
对我来说这似乎是JSON,您是否尝试了JSON\u decode()
?确切的响应是什么?这看起来像是php和js的组合。不使用get\u object\u vars()
您只需使用json\u decode($string,true)代码>。这将返回一个数组而不是对象。谢谢@ameenulla0007
<?php
$pp = json_decode('{
"data": {
"first_name": "Dany",
"last_name": "mate",
"id": "1379933133290837510",
"image": {
"60x60": {
"url": "https://s-media-cache-ak0.pinimg.com/avatars/dtest_1438666564_60.jpg",
"width": 60,
"height": 60
}
}
}
}');
$data = json_encode($pp->{'data'});
$aa = json_decode($data);
echo $aa->{'first_name'} .' & ';
$image = json_encode($aa->{'image'});
$dd = json_decode($image);
$sity = json_encode($dd->{'60x60'});
$url = json_decode($sity);
echo $url->{'url'};
?>