我怎么能用PHP编写一个看起来像这个(嵌套的JSON结构)的JSON文件?

我想用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领土上的新手! :()


繁华开满天机
浏览 114回答 1
1回答

一只萌萌小番薯

你错过了一些小的关键点$knxGenSet = (object) [&nbsp; &nbsp; 'general' => (object) ["language" => $_POST["langsetts"],&nbsp;&nbsp; &nbsp; "workingmode" => $_POST["wmodesetts"]]];for($j=0; $j<2; $j++) {&nbsp; &nbsp; $scene = (object) [&nbsp; &nbsp; &nbsp; &nbsp; 'id' => $j,&nbsp; &nbsp; &nbsp; &nbsp; 'calendar_event' => $_POST["calendarevent" . $j]&nbsp; &nbsp; ];&nbsp; &nbsp; for($ii=0; $ii<2; $ii++) {&nbsp; &nbsp; &nbsp; &nbsp; $scene->element[] = (object) [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "device" => "boiler".$ii,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "active_state"=> "on",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "hybernation_state"=> "off",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "calendar_name"=> "roomlink4"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;];&nbsp; &nbsp; }&nbsp; &nbsp; $knxGenSet->scene[] = $scene;}print json_encode($knxGenSet);输出{&nbsp; &nbsp; "general": {&nbsp; &nbsp; &nbsp; &nbsp; "language": null,&nbsp; &nbsp; &nbsp; &nbsp; "workingmode": null&nbsp; &nbsp; },&nbsp; &nbsp; "scene": [{&nbsp; &nbsp; &nbsp; &nbsp; "id": 0,&nbsp; &nbsp; &nbsp; &nbsp; "calendar_event": null,&nbsp; &nbsp; &nbsp; &nbsp; "element": [{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "device": "boiler0",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "active_state": "on",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "hybernation_state": "off",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "calendar_name": "roomlink4"&nbsp; &nbsp; &nbsp; &nbsp; }, {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "device": "boiler1",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "active_state": "on",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "hybernation_state": "off",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "calendar_name": "roomlink4"&nbsp; &nbsp; &nbsp; &nbsp; }]&nbsp; &nbsp; }, {&nbsp; &nbsp; &nbsp; &nbsp; "id": 1,&nbsp; &nbsp; &nbsp; &nbsp; "calendar_event": null,&nbsp; &nbsp; &nbsp; &nbsp; "element": [{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "device": "boiler0",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "active_state": "on",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "hybernation_state": "off",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "calendar_name": "roomlink4"&nbsp; &nbsp; &nbsp; &nbsp; }, {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "device": "boiler1",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "active_state": "on",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "hybernation_state": "off",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "calendar_name": "roomlink4"&nbsp; &nbsp; &nbsp; &nbsp; }]&nbsp; &nbsp; }]}
打开App,查看更多内容
随时随地看视频慕课网APP