我正在尝试从以下输出中获取username和id的值。
{
"listusersresponse": {
"count": 1,
"user": [
{
"id": "f01e8ea4-2da5-11e4-a846-726c7bbb864f",
"username": "admin",
"firstname": "admin",
"lastname": "test",
"created": "2014-08-26T20:52:24-0400",
"state": "enabled",
"account": "admin",
"accounttype": 1,
"domainid": "c091153a-2da5-11e4-a846-726c7bbb864f",
"accountid": "f01e7c02-2da5-11e4-a846-726c7bbb864f",
"iscallerchilddomain": false,
"isdefault": true
}
]
}
}
这就是我试过的
$url = file_get_contents("http://URL/client/&response=json");
$data = json_decode($url);
var_dump($data);
foreach ($data as $value) {
echo $value->count;
}
对于这项工作,我得到1
但是在尝试访问“用户名”
时foreach ($data as $value) {
echo $value->user->username;
}
我收到以下错误:注意:尝试在“filename”中获取非对象的属性
答案 0 :(得分:2)
用户是一个数组。
foreach ($data as $value) {
echo $value->user[0]->username;
}
不确定你的json如何看待几个用户,它必须是这样的,我猜:
foreach ($data as $value) {
foreach ($value->user as $user) {
echo $user->username;
}
}