我正在为我的json文件设置一个简单的PHP处理程序。
这是我的设置,我不确定我需要在PHP脚本中定义什么才能从json的长列表中获得这个ID。
如有任何建议或帮助,将不胜感激。
<?php
$id = $_GET['id']; //get ?id=
$jsonurl = "api/documents.json"; //json path
$json = file_get_contents($jsonurl); //getting file
$decode = json_decode($json); //decoding the json
$echome = $decode[0]->$id; //looking for "id" within the json
$reencode = json_encode($echome) //re-encoding this segmented json
echo($reencode); //echo the json期望的结果是
//load page with id set as 21
{
"21": {
"name": "mike",
"active": "yes"
}
}url = www.example.com/process.php?id=21
// simple example of the json
{
"20": {
"name": "john",
"active": "no"
},
"21": {
"name": "mike",
"active": "yes"
}
}发布于 2019-03-28 14:18:20
$decode不是数组,而是对象,所以最好将其解码为数组,然后按如下方式访问键:
$id = $_GET['id'];
$decode = json_decode($json, true);
$echome = $decode[$id];注意,true是json_decode()接受的第二个参数。您可以阅读更多关于它的这里。
发布于 2019-03-28 14:16:22
如果您想以数组的形式访问它,通过将true传递给json_decode解码为关联数组,那么:
$echome = $decode[$id]; //looking for "id" within the json或者,如果希望将其保留为对象,则可以执行以下操作来访问这些属性:
$echome = $decode->{$id}; //looking for "id" within the jsonhttps://stackoverflow.com/questions/55399750
复制相似问题