【问题标题】:How to parse a Java List of already parsed JSON into a Big JSON?如何将已解析 JSON 的 Java 列表解析为大 JSON?
【发布时间】:2019-08-30 18:51:09
【问题描述】:

我使用Jacksonserialize/deserialize JSON

我有一个List<String>,其中的所有元素都已经是serializedJSON 格式。我想从那个List 生成一个大的JSON

换句话说,我有:

List<String> a = new ArrayList<>();
a[0] = JSON_0
a[1] = JSON_1
...
a[N] = JSON_N

我想渲染:

[
   {JSON_0},
   {JSON_1},
   ...
   {JSON_N}
]

使用Jackson 的最佳方法是什么?

【问题讨论】:

    标签: java json serialization jackson json-serialization


    【解决方案1】:

    可能更简单的解决方案是创建ArrayNode 并使用addRawValue 方法:

    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.databind.node.ArrayNode;
    import com.fasterxml.jackson.databind.util.RawValue;
    
    public class JsonApp {
    
        public static void main(String[] args) throws Exception {
            ObjectMapper mapper = new ObjectMapper();
    
            ArrayNode nodes = mapper.getNodeFactory().arrayNode();
            nodes.addRawValue(new RawValue("{}"));
            nodes.addRawValue(new RawValue("true"));
            nodes.addRawValue(new RawValue("{\"id\":1}"));
    
            System.out.println(mapper.writeValueAsString(nodes));
        }
    }
    

    上面的代码打印:

    [{},true,{"id":1}]
    

    您还可以使用列表创建POJO 并使用@JsonRawValue 注释。但是如果你不能有额外的根对象,你需要为它实现自定义序列化器。 POJO 和自定义序列化程序的示例:

    import com.fasterxml.jackson.core.JsonGenerator;
    import com.fasterxml.jackson.databind.JsonSerializer;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.databind.SerializerProvider;
    import com.fasterxml.jackson.databind.annotation.JsonSerialize;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    public class JsonApp {
    
        public static void main(String[] args) throws Exception {
            ObjectMapper mapper = new ObjectMapper();
    
            List<String> jsons = new ArrayList<>();
            jsons.add("{}");
            jsons.add("true");
            jsons.add("{\"id\":1}");
    
            RawJsons root = new RawJsons();
            root.setJsons(jsons);
            System.out.println(mapper.writeValueAsString(root));
        }
    }
    
    @JsonSerialize(using = RawJsonSerializer.class)
    class RawJsons {
    
        private List<String> jsons;
    
        public List<String> getJsons() {
            return jsons;
        }
    
        public void setJsons(List<String> jsons) {
            this.jsons = jsons;
        }
    }
    
    class RawJsonSerializer extends JsonSerializer<RawJsons> {
    
        @Override
        public void serialize(RawJsons value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
            gen.writeStartArray();
            if (value != null && value.getJsons() != null) {
                for (String json : value.getJsons()) {
                    gen.writeRawValue(json);
                }
            }
            gen.writeEndArray();
        }
    }
    

    如果您需要为数组中的所有项目启用SerializationFeature.INDENT_OUTPUT 功能,则需要反序列化所有内部对象并再次序列化它们。

    另见:

    【讨论】:

    • 就是这样!感谢您的时间。这很有帮助。
    【解决方案2】:

    字符“[”这一简单事实我们将其标记为一个数组,因此我建议将列表放入 JSON 数组中。

    我需要更多信息来帮助你,因为使用 JSON 字符串没有多大意义,因为 JSON 是由 Key/Value 组成的,最好用属性制作一个 bean/object . 示例:

    class Object {
        private String attribute = value;
    }
    
    {attribute: value}
    

    【讨论】:

      猜你喜欢
      • 2012-04-18
      • 2021-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多