我正在尝试解析完整的JSON结构。我已经能够获得单独的嵌套对象,但我希望它比查找每个嵌套结构简单得多。
这是我的JSON结构:
{
"request1": {
"nest11": "solution11",
"nest12": "solution12"
},
"request2": {
"nest21": "solution21",
"nest22": "solution22"
},
"request3": true,
"request3": {
"nest31": "solution31",
"nest32": "solution31",
"nestnest1": {
"nestnest11": "nestsolution11",
"nestnest12": "nestsolution12"
},
"nestrequest2": {
"nestrequest21": [{
"nestnest21": "solution21",
"nestnest22": "solution22"
}]
},
"request4": "solution4"
}
}
当我从API获得响应时,让我们说$ serverResponse,我正在解码以获取对象
$newobj = json_decode($serverResponse);
foreach ($newobj->request1 as $key=>$value) {
$request1 = "request1.".$key.": <b>". $value."</b><br/>";
}
然后我将它发送回前端进行打印。问题是我该怎么做,所以我不必遍历每个对象并获得单个值。我需要打印所有键值,无论是否嵌套。感谢您的任何指示和帮助!
答案 0 :(得分:1)
IUnitOfWork
function recursivePrinter($data){
foreach($data as $k=>$v){
if(is_array($v)) recursivePrinter($v);
else echo "$k - $v";
}
}
recursivePrinter(json_decode($response, true));