我想在PHP中添加一个json对象到其他json对象,我尝试了这个和许多其他方法,但我找不到正确的方法
This is What I have:
$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相同的结构,类似于:
{"info":[{....}, {$js_string2}]}
你的代码很好,两个JSON字符串都不是。它们都有一个额外的"]}"结尾。
执行代码会引发警告array_merge(): Argument #1 is not an array
。那应该能让你明白原因。
编辑
array_merge
创建一个包含(在您的示例中)第一个和第二个数组的所有键的新数组。这些键是"info"
和"thumb"
。结果(同样是JSON)看起来类似于{"info": ..., "thumb": ...}
。
你真正想要的是将第二个数组添加到第一个数组的信息数组中,即执行以下操作。
$result = json_decode($js_string1, true);
$result["info"][] = json_decode($js_string2, true);
只要你的JSON字符串是有效的(不幸的是,它们在每个线程的末尾包含额外的
]}
),你可以这样做:
<?php
$data1 = json_decode($js_string1);
$data2 = json_decode($js_string2);
$data1->info[] = $data2; //<== THIS WILL SET $data2 INTO
//<== THE "info" ARRAY OF $data1
请注意,您的JSON字符串被剥夺了一些无关的]}在每个字符串的末尾…这里有一个快速测试:
<?php
$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
}'; //]}
$data1 = json_decode($js_string1);
$data2 = json_decode($js_string2);
$data1->info[] = $data2; //<== THIS WILL SET $data2
//<== INTO THE "info" ARRAY OF $data1
var_dump($data1);
// YIELDS::
object(stdClass)[4]
public 'info' =>
array (size=2)
0 =>
object(stdClass)[1]
public 'thumb' => string '' (length=0)
public 'count' => int 1
public 'date' => string '11/11/2016 4:05:28' (length=18)
public 'categories' =>
array (size=5)
...
public 'sharing' => string '' (length=0)
public 'status' => string 'private' (length=7)
public 'title' => string 'Apple' (length=5)
public 'windows' =>
array (size=2)
...
public 'top' => int 26
public 'type' => string 'normal' (length=6)
public 'width' => int 1440
1 =>
object(stdClass)[5]
public 'thumb' => string '' (length=0)
public 'count' => int 1
public 'date' => string '10/10/2010 5:07:30' (length=18)
public 'categories' =>
array (size=5)
...
public 'sharing' => string '' (length=0)
public 'status' => string 'private' (length=7)
public 'title' => string 'Some Title' (length=10)
public 'windows' =>
array (size=2)
...
public 'top' => int 26
public 'type' => string 'normal' (length=6)
public 'width' => int 1024