Creating a json object using jackson

前端 未结 4 1994
孤城傲影
孤城傲影 2020-12-05 13:26

How can I create a json array like the example below using jackson.

I tried using ObjectMapper, but this does not seem correct.

      try (Director         


        
4条回答
  •  没有蜡笔的小新
    2020-12-05 13:50

    You can do this without creating POJO and converting it into JSON. I assume your data in the Record object.

            JsonNode rootNode = mapper.createObjectNode();
            ArrayNode childNodes = mapper.createArrayNode();
            for (Record record : records) {
                JsonNode element = mapper.createObjectNode();
                ((ObjectNode) element).put("mime": record.getDirectory());
                      //fill rest of fields which are needed similar to this.
                      //Also here record.getDirectory() method will should return "directory"
                      //according to your json file.
                childNodes.add(element);
            }
            ((ObjectNode) rootNode).put("files", childNodes);
    

提交回复
热议问题