将现有数组中的嵌套数组添加到json文件中

时间:2019-02-28 14:06:08

标签: java json jackson

尝试将嵌套数组添加到JSON中的数组中。

 public SecondArray createOrUpdate(SecondArray object) {
  try {
   ObjectMapper mapper = new ObjectMapper();
   ObjectNode root = (ObjectNode) mapper.readTree(jsonFile);
   ObjectNode newArrayElement = new ObjectNode(mapper.getNodeFactory());
   ObjectNode newItem = new ObjectNode(mapper.getNodeFactory());
   ArrayNode secondArr = (ArrayNode) root.get("secondArray");
   newArrayElement.put("number", object.getNumber());
   newArrayElement.put("data", object.getData);

   for (SecondArray.Item items: object.getItems()) {
    newItem.put("item1", items.getItem1));
   newItem.put("item2", items.getItem2);
  }
  secondArr.add(mapper.convertValue(newArrayElement, JsonNode.class));
  return object;
 } catch (IOException e) {
  e.printStackTrace();
 }
 }

具有此输入JSON

{
   "firstArray":[
      {
         "data1":1,
         "data2":"DATA"
      },
      {
         "data1":2,
         "data2":"DATA2"
      }
   ],
   "secondArray":[
      {
         "number":1,
         "data":"DATA3",
         "items":[
            {
               "item1":"sword",
               "item2":"mace"
            },
            {
               "item1":"dagger",
               "item2":"axe"
            }
         ]
      }
   ]
}

需要向具有相同属性的现有secondArray添加一个新对象。此刻我正在得到这样的东西:

{
   "firstArray":[
      {
         "data1":1,
         "data2":"DATA"
      },
      {
         "data1":2,
         "data2":"DATA2"
      }
   ],
   "secondArray":[
      {
         "number":1,
         "data":"DATA3",
         "items":[
            {
               "item1":"sword",
               "item2":"mace"
            },
            {
               "item1":"dagger",
               "item2":"axe"
            }
         }
      ],
      {
         "number":2,
         "data":"DATA4",
         "items":[
            {
               "item1":"NEWITEM3",
               "item2":"NEWITEM4"
            }
         ]
      }
   }
]
}

如何获取代码以将newArrayElement添加到secondArraynewArrayElement中,其中newItemsnewArrayelement内至少有2个docker stop $(docker ps -a -q)数组?

1 个答案:

答案 0 :(得分:0)

您需要为集合中的每个newItem创建一个Item。另外,请记住将其添加到数组的末尾。在下面,您可以找到示例,该示例为给定的JSON有效负载添加了新的item

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.io.File;
import java.util.Arrays;
import java.util.List;

public class JsonApp {

    public static void main(String[] args) throws Exception {
        File jsonFile = new File("./resource/test.json").getAbsoluteFile();

        ObjectMapper mapper = new ObjectMapper();
        mapper.enable(SerializationFeature.INDENT_OUTPUT);

        SecondArray secondArray = new SecondArray();

        ObjectNode root = (ObjectNode) mapper.readTree(jsonFile);
        ArrayNode array = (ArrayNode) root.get("secondArray");

        // create new item
        ObjectNode newObject = mapper.createObjectNode();
        newObject.put("number", 2);
        newObject.put("data", "DATA4");

        // new array for items
        ArrayNode items = mapper.createArrayNode();
        for (SecondArray.Item item : secondArray.getItems()) {
            // new item
            ObjectNode newItem = mapper.createObjectNode();
            newItem.put("item1", item.getItem1());
            newItem.put("item2", item.getItem2());

            // add item to array
            items.add(newItem);
        }

        // add array to new object
        newObject.set("items", items);

        // add created object to secondArray
        array.add(newObject);

        System.out.println(mapper.writeValueAsString(root));
    }
}