java - 如何将已解析的 JSON 的 Java 列表解析为 Big JSON?

标签 java json serialization jackson json-serialization

我使用Jacksonserialize/deserialize JSON .

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

换句话说,我有:

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

我想渲染:

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

使用 Jackson 执行此操作的最佳方法是什么? ?

最佳答案

可能更简单的解决方案是创建 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 功能,则需要反序列化所有内部对象并再次序列化它们。

另请参阅:

关于java - 如何将已解析的 JSON 的 Java 列表解析为 Big JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57731892/

相关文章:

Javascript:反序列化对象和混合方法

firebase - Kotlin - 可序列化类中的初始化 block 只能读取默认属性值

java - 项目正在寻找数据源,但我正在使用 MongoDB

java - 执行 HTTP.PUT 的正确方法

javascript - 我如何读取 Web 服务的以下 json 响应?

c - 未定义对 "tpl_map"的引用

java - SimpleDateFormat 无法解释的 ParseException

java - 域对象和 Controller 类

json - 使用@JsonFormat 时的错误时间(+1 小时)

python - Variable.get ('x' , deserialize_json=True) 与 Jinja '{{ var.json.x }}' 相同吗?