如何使用PHP在给定的json文件中的特定位置插入/删除节点

时间:2019-05-10 06:22:22

标签: php json

我需要向深度嵌套的json文件添加一个新节点。我不知道如何获取要插入节点的数组索引。

这是用于访问和管理JSON文件中的抵押品。我尝试使用array_split()函数,但是第三个参数仅添加具有一个值的节点。

示例JSON文件:

{
   "IsNew":"0",
   "Title":"Industry",
   "View":"grid",
   "File":"",
   "Items":[
      {
         "IsNew":"0",
         "Title":"Industrial Products",
         "Image":"Industrial-Products.png",
         "View":"list",
         "Items":[
            {
               "IsNew":"0",
               "Title":"Offerings",
               "View":"detaillist",
               "Items":[

               ]
            },
            {
               "IsNew":"0",
               "Title":"Collateral",
               "View":"multidetaillist",
               "Items":[
                  {
                     "IsNew":"0",
                     "Title":"Flyers and Brochures",
                     "Subtitle":"",
                     "Items":[
                        {
                           "IsNew":"0",
                           "Title":"Building Solutions",
                           "View":"pdf",
                           "File":"Building_Solutions.pdf",
                           "Type":"1"
                        },
                        {
                           "IsNew":"0",
                           "Title":"Industrial Machinery",
                           "View":"pdf",
                           "File":"Industrial_Machinery.pdf",
                           "Type":"1"
                        }
                     ]
                  }
               ]
            }
         ]
      }
   ]
}

要添加传单和小册子项目的节点:

 {
      "IsNew": "0",
      "Title": "abc",
      "Image": "abc",
      "View": "pdf",
      "File": "abc",
      "Type": "1"
 }

2 个答案:

答案 0 :(得分:1)

注意:您的原始样本JSON文件无效。它缺少一些封闭的括号,我已在下面的代码中进行了修复。

用于追加项目的php代码为:

std::this_thread::get_id()

这将导致:

$jsonStr = '{
    "IsNew": "0",
  "Title": "Industry",
  "View": "grid",
  "File": "",
  "Items": [
    {
        "IsNew": "0",
      "Title": "Industrial Products",
      "Image": "Industrial-Products.png",
      "View": "list",
      "Items": [
    {
        "IsNew": "0",
          "Title": "Offerings",
          "View": "detaillist",
          "Items": []
        }
        ,
     {
         "IsNew": "0",
          "Title": "Collateral",
          "View": "multidetaillist",
          "Items": [
          {
              "IsNew": "0",
              "Title": "Flyers and Brochures",
              "Subtitle": "",
              "Items": [
                 {
                     "IsNew": "0",
                  "Title": "Building Solutions",
                  "View": "pdf",
                  "File": "Building_Solutions.pdf",
                  "Type": "1"
                },
                {
                    "IsNew": "0",
                  "Title": "Industrial Machinery",
                  "View": "pdf",
                  "File": "Industrial_Machinery.pdf",
                  "Type": "1"
                }
             ]
}]}]}]}';

$insertStr = '{
      "IsNew": "0",
      "Title": "abc",
      "Image": "abc",
      "View": "pdf",
      "File": "abc",
      "Type": "1"
 }';

// turns json strings into objects
$obj = json_decode($jsonStr);
$insertObj = json_decode($insertStr);


// target the node is $obj->Items[0]->Items[1]->Items[0]->Items
// so we just append the object to the node
$obj->Items[0]->Items[1]->Items[0]->Items[] = $insertObj;

$newJson = json_encode($obj);
echo($newJson);

答案 1 :(得分:-1)

尝试这个json ...

{
  "name": "react-todo-app",
  "version": "0.1.0",
  "private": true,
  "homepage": "https://maksymilianMroz.github.io/react-todo-app",
  ...
}