1

我正在使用 The Echo Nest API 来寻找类似的艺术家。响应如下所示:

{"response": {"status": {"version": "4.2", "code": 0, "message": "Success"}, "artists": [{"name": "Audio Adrenaline", "id": "ARGEZ5E1187FB56F38"}, {"name": "Tree63", "id": "ARWKO2O1187B9B5FA7"}]}}

我怎样才能把它产生的艺术家放到一个数组中?所以我可以稍后回显它们,例如:

echo $artist[0];
4

3 回答 3

5

您只需要将json_decode()第二个参数设置为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
        )

)

键盘!

于 2013-09-10T19:00:54.453 回答
2

json_decode()是你需要的

$artist = json_decode($json);

或作为关联数组

$artist = json_decode($json, true);
于 2013-09-10T19:00:44.900 回答
0

使用json_decode,在这里找到。

样本:

$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)
}
于 2013-09-10T19:02:26.533 回答