将对象添加到json文件中的对象的JsonArray

时间:2019-02-27 14:57:54

标签: java json jackson

这是我的JSON负载,我需要向firstArray添加更多对象。

{
  "firstArray": [{
    "data1": 1,
    "data2": "DATA"
  }, {
    "data1": 2,
    "data2": "DATA2"
  }],
  "secondArray": [{
    "number": 1,
    "data": "DATA3",
  }, {
    "number": 2,
    "data": "DATA4"
  }],
  "thirdArray": [{
    "something": 1,
    "thatthing": "DATA5"
  }, {
    "something": 2,
    "thatthing": "DATA6"
  }]
}

这是我读取上方负载的方法:

public FirstArray createFirstArray(FirstArray object) {
 try {
  JsonNode jsonNode = mapper.readTree(new File(filePath));
  ObjectNode objectNode = mapper.getNodeFactory().objectNode();
  objectNode.putPOJO("firstArray", jsonNode.get("firstArray"));
  objectNode.put("data1", object.getData1());
  objectNode.put("data2", object.getData2());

  System.out.println(objectNode.toString());
 } catch (IOException e) {
  e.printStackTrace();
 }
}

它返回此:

{
   "firstArray":[
  {
     "data1":1,
     "data2":"DATA"
  },
  {
     "data1":2         "data2":"DATA2"
  }
   ],
   "data1":123,
   "data2":"randomDATA"
}

我需要获取数组中的数据,因为最后一个对象不是分开的,我该怎么做?

1 个答案:

答案 0 :(得分:0)

您需要存储根节点。在您的示例中,打印jsonNode变量。下面的示例执行相同的操作:

import com.fasterxml.jackson.databind.JsonNode;
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;

public class Json {

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

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

        ObjectNode root = (ObjectNode) mapper.readTree(jsonFile);

        // create new node item
        ObjectNode newNode = new ObjectNode(mapper.getNodeFactory());
        newNode.put("data1", 3);
        newNode.put("data2", "DATA3");

        // create new class item
        FirstItem newItem = new FirstItem(4, "DATA4");

        // get array node
        ArrayNode firstArray = (ArrayNode) root.get("firstArray");

        // add new ObjectNode
        firstArray.add(newNode);

        // add new POJO
        firstArray.add(mapper.convertValue(newItem, JsonNode.class));

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

class FirstItem {

    private int data1;
    private String data2;

    public FirstItem(int data1, String data2) {
        this.data1 = data1;
        this.data2 = data2;
    }

    public int getData1() {
        return data1;
    }

    public String getData2() {
        return data2;
    }
} 

上面的代码显示:

{
  "firstArray" : [ {
    "data1" : 1,
    "data2" : "DATA"
  }, {
    "data1" : 2,
    "data2" : "DATA2"
  }, {
    "data1" : 3,
    "data2" : "DATA3"
  }, {
    "data1" : 4,
    "data2" : "DATA4"
  } ],
  "secondArray" : [ {
    "number" : 1,
    "data" : "DATA3"
  }, {
    "number" : 2,
    "data" : "DATA4"
  } ],
  "thirdArray" : [ {
    "something" : 1,
    "thatthing" : "DATA5"
  }, {
    "something" : 2,
    "thatthing" : "DATA6"
  } ]
}