如何将已解析的JSON的Java列表解析为Big JSON? [英] How to parse a Java List of already parsed JSON into a Big JSON?
问题描述
我使用Jackson
到serialize/deserialize
JSON
.
我有一个List<String>
,其中所有内部元素已经以JSON
格式serialized
.我想从该List
生成一个大的JSON
.
I have a List<String>
in which all elements inside are already serialized
in JSON
format. I would like to generate a big JSON
from that 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
的最佳方法是什么?
What is the best way to do so using Jackson
?
推荐答案
Probably the simpler solution would be to create ArrayNode
and use addRawValue method:
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
和自定义序列化程序的示例:
You can also, create a POJO
with list and use @JsonRawValue
annotation. But if you can not have extra root object you need to implement custom serialiser for it. Example with POJO
and custom serialiser:
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
功能,则需要对所有内部对象进行反序列化,然后再次对其进行序列化.
If you need to have SerializationFeature.INDENT_OUTPUT
feature enabled for all items in array, you need to deserialise all inner objects and serialise them again.
另请参阅:
- How can I include raw JSON in an object using Jackson?
- Handling raw JSON values using Jackson
这篇关于如何将已解析的JSON的Java列表解析为Big JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!