处理JSON提要中的数据以显示它 [英] Manipulating data from a JSON feed to display it
问题描述
{
"Group": [
{
"name": "HolderOne",
"operators": [
{
"username": "ken",
"status": 3
},
.....etc.....
我尝试操作的JSON提要必须在上面设置格式.
The JSON feed I am attempting to manipulate has to format above.
我希望能够显示用户名和状态.
I wish to be able to display username and status.
$json = file_get_contents("urlhere");
$obj=json_decode($json);
echo $obj->username;
echo $obj->status;
这显然不起作用,因为它们在供稿中嵌套了(?)...我尝试过:
This obviously doesn't work as they are nested(?) within the feed...I have tried:
$obj->Group[0]->name->operators->username
和
$obj->Group[0]->name->username
无济于事(以及带有,true和['name']的json_decode等).
to no avail (as well as json_decode with ,true and ['name'], etc).
我特别昏暗吗?
当我执行var转储时,可以从feed中收集数据了.
when I do a var dump, the data is being collected from the feed okay.
推荐答案
弄清这一点的最好方法是迭代地执行print_r
的操作:
The best way to figure this out is to iteratively do print_r
's:
print_r($obj)
//prints what you see above
print_r($obj['Group']
//prints the Group Object
print_r($obj['Group'][0])
//prints first element in Group Object
print_r($obj['Group'][0]['operators'])
//etc.....
这就是我发现遇到困难时如何访问这些 deep 元素的方法.虽然在我看来您想要:
That's how I find out how to access these deep elements if I get a little stuck. Though it appears to me that you want:
$obj->Group[0]->operators[0]->username
这篇关于处理JSON提要中的数据以显示它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!