对PHP数组的JSON响应
我正在使用Echo Nest API寻找类似的艺术家。答复如下:对PHP数组的JSON响应,php,json,Php,Json,我正在使用Echo Nest API寻找类似的艺术家。答复如下: {"response": {"status": {"version": "4.2", "code": 0, "message": "Success"}, "artists": [{"name": "Audio Adrenaline", "id": "ARGEZ5E1187FB56F38"}, {"name": "Tree63", "id": "ARWKO2O1187B9B5FA7"}]}} 我如何才能将艺术家的结果放入阵列中?所
{"response": {"status": {"version": "4.2", "code": 0, "message": "Success"}, "artists": [{"name": "Audio Adrenaline", "id": "ARGEZ5E1187FB56F38"}, {"name": "Tree63", "id": "ARWKO2O1187B9B5FA7"}]}}
我如何才能将艺术家的结果放入阵列中?所以我以后可以像这样回显它们:
echo $artist[0];
这是你需要的
$artist = json_decode($json);
或作为关联数组
$artist = json_decode($json, true);
这是你需要的
$artist = json_decode($json);
或作为关联数组
$artist = json_decode($json, true);
只需将第二个参数设置为TRUE
,即可使用
$str = '...';
$json = json_decode($str, TRUE);
$artist = $json['response']['artists'];
//$artist = json_decode($str, TRUE)['response']['artists']; as of PHP 5.4
print_r($artist);
输出:
Array
(
[0] => Array
(
[name] => Audio Adrenaline
[id] => ARGEZ5E1187FB56F38
)
[1] => Array
(
[name] => Tree63
[id] => ARWKO2O1187B9B5FA7
)
)
您只需将第二个参数设置为TRUE
$str = '...';
$json = json_decode($str, TRUE);
$artist = $json['response']['artists'];
//$artist = json_decode($str, TRUE)['response']['artists']; as of PHP 5.4
print_r($artist);
输出:
Array
(
[0] => Array
(
[name] => Audio Adrenaline
[id] => ARGEZ5E1187FB56F38
)
[1] => Array
(
[name] => Tree63
[id] => ARWKO2O1187B9B5FA7
)
)
使用
json\u解码
,找到
样本:
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
输出:
object(stdClass)#1 (5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}
使用
json\u解码
,找到
样本:
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
输出:
object(stdClass)#1 (5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}