为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
繁华开满天机 2022-09-30 16:21:35
我想用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领土上的新手! :()
查看完整描述

1 回答

?
一只萌萌小番薯

TA贡献1795条经验 获得超7个赞

你错过了一些小的关键点


$knxGenSet = (object) [

    'general' => (object) ["language" => $_POST["langsetts"], 

    "workingmode" => $_POST["wmodesetts"]

]

];


for($j=0; $j<2; $j++) {

    $scene = (object) [

        'id' => $j,

        'calendar_event' => $_POST["calendarevent" . $j]

    ];


    for($ii=0; $ii<2; $ii++) {

        $scene->element[] = (object) [

            "device" => "boiler".$ii,

            "active_state"=> "on",

            "hybernation_state"=> "off",

            "calendar_name"=> "roomlink4"

         ];

    }

    $knxGenSet->scene[] = $scene;

}


print json_encode($knxGenSet);


输出


{

    "general": {

        "language": null,

        "workingmode": null

    },

    "scene": [{

        "id": 0,

        "calendar_event": null,

        "element": [{

            "device": "boiler0",

            "active_state": "on",

            "hybernation_state": "off",

            "calendar_name": "roomlink4"

        }, {

            "device": "boiler1",

            "active_state": "on",

            "hybernation_state": "off",

            "calendar_name": "roomlink4"

        }]

    }, {

        "id": 1,

        "calendar_event": null,

        "element": [{

            "device": "boiler0",

            "active_state": "on",

            "hybernation_state": "off",

            "calendar_name": "roomlink4"

        }, {

            "device": "boiler1",

            "active_state": "on",

            "hybernation_state": "off",

            "calendar_name": "roomlink4"

        }]

    }]

}


查看完整回答
反对 回复 2022-09-30
  • 1 回答
  • 0 关注
  • 112 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号