我有一个私有的 JSON 提要(按 IP 地址),我可以使用命令解析它
$json = file_get_contents("http://#");
这就是提要的外观和命令的工作原理 - 当使用json_decode($json)时,它会正确转储数据;或json_decode($json,true);我做了一个var转储。
{
"Holder": [
{
"name": "Subholder One",
"operators": [
{
"username": "User1",
"status": 3
},
{
"username": "User2",
"status": 3
},
{
"username": "User3",
"status": 3
},
{
"username": "User4",
"status": 1
}
]
},
{
"name": "Subholder Two",
"operators": [
{
"username": "User5",
"status": 3
}
]
},
{
"name": "Subholder Three",
"operators": [
{
"username": "User6",
"status": 3
}
]
},
{
"name": "Subholder Four",
"operators": [
{
"username": "User7",
"status": 1
},
{
"username": "User8",
"status": 3
},
{
"username": "User9",
"status": 1
}
]
}
]
}
当我使用简单的以下代码和一行数据时,没有file_get_contents,只有一行数据,它可以工作:
$obj = json_decode($json);
echo $obj->username;
echo $obj->status;
当我使用file_get_contents时,它不起作用。
我希望能够做的是为所有数据显示用户名和状态(转换为我已完成的短语的数字),并按用户名单独显示。
例如,在用户 1 的个人资料页面上,我想解析他们的"用户名"和"状态",将状态编号转换为相应的短语并显示在屏幕上。
我在这里挠头...所以任何帮助都感激不尽。
试试
$obj = json_decode($json, true);
echo $obj['username'];
echo $obj['status'];
试试这个:
$request_url ="http://mysamplefeed.com/";
$requests = file_get_contents($request_url);
$my_response = json_decode($requests);
foreach($my_response->Holder as $item){
'<p>' .$item->username; . '</p>'
'<p>' .$item->status; . '</p>'
}