我收到了这个请求.
{ "area": [
{
"area": "kothrud"
},
{
"area": "katraj"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我想通过根据上述请求在数据库中搜索记录来提供对此的响应.我将如何解码上面的json数组并分别使用每个区域字段.
ita*_*chi 26
你的字符串不是一个有效的json开头.
一个有效的json,
{
"area": [
{
"area": "kothrud"
},
{
"area": "katraj"
}
]
}
Run Code Online (Sandbox Code Playgroud)
如果你做了json_decode,它会产生,
stdClass Object
(
[area] => Array
(
[0] => stdClass Object
(
[area] => kothrud
)
[1] => stdClass Object
(
[area] => katraj
)
)
)
Run Code Online (Sandbox Code Playgroud)
更新:使用
$string = '
{
"area": [
{
"area": "kothrud"
},
{
"area": "katraj"
}
]
}
';
$area = json_decode($string, true);
foreach($area['area'] as $i => $v)
{
echo $v['area'].'<br/>';
}
Run Code Online (Sandbox Code Playgroud)
输出:
kothrud
katraj
Run Code Online (Sandbox Code Playgroud)
更新#2:
为此true:
如果为TRUE,则返回的对象将转换为关联数组.有关更多信息,请单击此处
你可以使用json_decode功能
foreach (json_decode($response) as $area)
{
print_r($area); // this is your area from json response
}
Run Code Online (Sandbox Code Playgroud)
看到这个小提琴
| 归档时间: |
|
| 查看次数: |
84612 次 |
| 最近记录: |