无法在PHP中合并两个JSON对象,结果为null

时间:2016-11-13 09:24:05

标签: php arrays json

我想在PHP中将json对象添加到其他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保留相同的结构,如:

{" info":[{....},{$ js_string2}]}

2 个答案:

答案 0 :(得分:2)

您的代码很好,两个JSON字符串都没有。他们最后都有一个额外的“]}”。

执行代码会发出警告array_merge(): Argument #1 is not an array。这应该让你找到原因。

修改

array_merge创建一个新数组,包含(在您的情况下)第一个和第二个数组的所有键。这些密钥为"info""thumb"。结果(再次作为JSON)看起来像{"info": ..., "thumb": ...}

你真正想要的是将第二个数组添加到第一个数组的info-array中,即执行以下操作。

$result = json_decode($js_string1, true);
$result["info"][] = json_decode($js_string2, true);

答案 1 :(得分:1)

  

只要您的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字符串在每个字符串的末尾都被剥夺了一些无关的]} ... A Quick-Test Here

    <?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