我想用PHP编写一个“嵌套”的JSON结构 下面看看结果应该是什么样子:
(手动编写 - 可以在JS中读取而不会出错:))
{
"general": {
"language": "English",
"workingmode": "Normal"
},
"scene": [{
"id": 0,
"calendar_event": "urlaub",
"element": [
{
"device": "boiler1",
"active_state": "nothing",
"hybernation_state": "off",
"calendar_name": "roomlink4"
},
{
"device": "boiler1",
"active_state": "on",
"hybernation_state": "off",
"calendar_name": "roomlink4"
}
]
},
{
"id": 1,
"calendar_event": "urlaub",
"element": [
{
"device": "boiler1",
"active_state": "on",
"hybernation_state": "off",
"calendar_name": "roomlink4"
},
{
"device": "boiler1",
"active_state": "on",
"hybernation_state": "off",
"calendar_name": "roomlink4"
}
]
}
]
}
这是我到目前为止的代码:
$knxGenSet = (object) [
'general' => (object) [],
'scene' => []
//Now i Struggle with this part, i cannot accomplish it to add "element" so that it is nested inside of "scene"
];
//creating structure for "general"
$knxGenSet->general->language = $_POST["langsetts"];
$knxGenSet->general->workingmode = $_POST["wmodesetts"];
//second part which i struggle with
$knxGenSet->scene[] = (object) [
//creating structure for "scene"
'id' => 0,
'calendar_event' => "anything"
//Struggle: cannot get it right to add "element"-array which consists of 4 more String to the structure
];
我确实设法添加了“常规”选项卡和“场景”的一部分,但我没有设法将“元素”数组的部分添加到“场景”
顺便说一句,我可以用JS阅读这个json格式
提前致谢(json领土上的新手! :()
一只萌萌小番薯