我正在尝试获取某人连接的蒸汽组的ID。这是JSON输出:
{
"response": {
"success": true,
"groups": [
{
"gid": "111"
},
{
"gid": "222"
},
{
"gid": "333"
},
{
"gid": "444"
},
{
"gid": "555"
}
]
}
}
我尝试了通过:
$groupIDs = $reply['response']['groups'];
foreach ($groupIDs as $gID) {
// Do stuff
}
我遇到以下错误,但我正在努力查看如何纠正它。
Invalid argument supplied for foreach()
对不起,我没有清楚。我已经在foreach()之前对其进行解码。
$reply = json_decode($reply, true);
首先,您必须使用PHP函数JSON_DECODE解码JSON字符串。然后迭代对象,如下所示
$string = '{
"response": {
"success": true,
"groups": [
{
"gid": "111"
},
{
"gid": "222"
},
{
"gid": "333"
},
{
"gid": "444"
},
{
"gid": "555"
}
]
}
}';
$array = json_decode($string);
foreach($array->response->groups as $value ){
echo $value->gid;
echo "<br/>";
}
http://php.net/manual/pt_br/function.json-decode.php
尝试:
$response = json_decode($reply, true);
$groupIDs = $response['response']['groups'];
foreach ($groupIDs as $gID) {
// Do stuff
}
使用json_decode($ wendesp)cf doc
$rep = json_decode($response)
foreach ($rep->response->groups as $gID) {
// Do stuff
}
$groups = $reply['response']->groups;
foreach ($groups as $group) {
print $group->gid;
}
在JSON中,每个 {}
表示为对象,每个 []
被解析为数组。