我想一個JSON對象添加到其他的JSON對象在PHP中,我想這和許多其他的方法,但我不能找到正確的方法無法合併PHP 2個JSON對象,結果爲空
這是我有什麼:
$js_string1 = "{\"info\":[{\"thumb\":\"\",\"count\":1,\"date\":\"11/11/2016 4:05:28\",\"categories\":[null,null,null,null,null],\"sharing\":\"\",\"status\":\"private\",\"title\":\"Apple\",\"windows\":[{\"alwaysOnTop\":false,\"focused\":true,\"width\":1440,\"windowId\":825},{\"active\":false,\"audible\":false, \"height\":727,\"width\":1440,\"windowId\":825}],\"top\":26,\"type\":\"normal\",\"width\":1440}]}";
$js_string2 = "{\"thumb\":\"\",\"count\":1,\"date\":\"10/10/2010 5:07:30\",\"categories\":[null,null,null,null,null],\"sharing\":\"\",\"status\":\"private\",\"title\":\"Some Title\",\"windows\":[{\"alwaysOnTop\":false,\"focused\":true,\"width\":1024,\"windowId\":201},{\"active\":false,\"audible\":false, \"height\":500,\"width\":1024,\"windowId\":301}],\"top\":26,\"type\":\"normal\",\"width\":1024}";
$result = json_encode(array_merge(json_decode($js_string1, true),json_decode($js_string2, true)));
預期的結果是:
{"info":[{"thumb":"","count":1,"date":"11/11/2016 4:05:28","categories":[null,null,null,null,null],"sharing":"","status":"private","title":"Apple","windows":[{"alwaysOnTop":false,"focused":true,"width":1440,"windowId":825},{"active":false,"audible":false, "height":727,"width":1440,"windowId":825}],"top":26,"type":"normal","width":1440}] }, {"thumb":"","count":1,"date":"10/10/2010 5:07:30","categories":[null,null,null,null,null],"sharing":"","status":"private","title":"Some Title","windows":[{"alwaysOnTop":false,"focused":true,"width":1024,"windowId":201},{"active":false,"audible":false, "height":500,"width":1024,"windowId":301}],"top":26,"type":"normal","width":1024}]} ]}
五月有人解釋,並告訴我如何正確地做到這一點?因爲我嘗試了很多不同的方式,而且我無法找到正確的方法。
我想要做的只是增加$ js_string2至$ js_string1並保持相同的結構$ js_string1,是這樣的:
{ 「信息」:[{......},{$ js_string2 }]}
'$ js_string1'和'$ js_string2'是無效的JSON字符串 – Federkun
什麼的var_dump輸出(array_merge(json_decode($ js_string1,真),json_decode($ js_string2,真))) ?如果這是null,那麼合併或解碼jsons就有問題。 –
是的,你是對的,json_last_error()返回JSON_ERROR_STATE_MISMATCH,但是什麼是錯的 – Fed