我们为我们的网站提供一个 API,返回格式为 XML 和 JSON。两者都来自 PHP 数组。我的 JSON 返回是导致我出现问题的原因。如何返回同名元素而不是 [0]、1等?现在,我返回的样子是:
{
"rate": {
"rate_span": "1 Minute",
"rate_limit": "60",
"rate_remaining": 59,
"rate_user": "60"
},
"status": {
"status_code": 200,
"status_message": "Request was successful."
},
"favorites": {
"0": {
"favorite": {
"lyric_id": "570"
}
},
"1": {
"favorite": {
"lyric_id": "3530822107858532003"
}
},
"2": {
"favorite": {
"lyric_id": "3530822107858535795"
}
},
"favorites_type": "lyrics",
"favorites_total": 3
}
}
我宁愿每个最爱都被包裹在它自己的“最爱” { ... 元素中,并绕过每个被包裹在 [0], 1 , [x+1].... 中的数组。就像这个 JSON 响应:ARTIST.ALBUMS .GET JSON
这是构建收藏夹部分的 PHP 数组...
$json["favorites"] = array();
$json["favorites"]["favorites_type"] = "lyrics";
$json["favorites"]["favorites_total"] = $userFavorites->recordCount();
foreach($userFavorites as $key => $fav) {
$song_id = $fav["lyric_id"];
$json["favorites"][]["favorite"]["lyric_id"] = $song_id;
}