[英]Is it possible do serialize/deserialize multilevel json objects?
When I have a simple json it is easy, take 当我有一个简单的json时,很容易
{"type":"simple","content":"i love apples"}
I just create a pojo: 我只是创建一个pojo:
public class Example {
public Object type;
public Object content;
}
Then doing: 然后做:
ObjectMapper mapper = new ObjectMapper();
Example ex = mapper.readValue(getInputStream(),Example.class)
will do the job. 会做的工作。
But now suppose I have something more complicated, a multilevel json 但是现在假设我有一个更复杂的东西,一个多级json
{
"type": "complicated",
"params": [
{
"type": "simple",
"content": "i still love apples"
},
{
"type": "simple",
"content":"i love spam too"
}
]
}
As you can see the "params" field of this new Object is a json array, and each element of this array could be mapped to my Example
pojo class. 如您所见,这个新Object的“ params”字段是一个json数组,并且该数组的每个元素都可以映射到我的Example
pojo类。
Is there a way to do this? 有没有办法做到这一点? Sorry if it could seem trivial, but I can't find any good documentation about jackson... it just talks about simple cases. 抱歉,看似微不足道,但我找不到关于杰克逊的任何好的文档……它只是在谈论简单的案例。
Here you go! 干得好!
NOTE: no setters in the class, which is why I have to use @JsonCreator
. 注意:类中没有设置器,这就是为什么我必须使用@JsonCreator
。 Habit of mine, I don't do beans ;) 我的习惯,我不做豆子;)
If you have setters for the different fields you can do without @JsonCreator
at all. 如果您有针对不同字段的设置器,则完全不需要@JsonCreator
。
public final class Jackson
{
private static final String JSONCONTENT
= "{" +
"\"type\":\"complicated\"," +
"\"params\":[" +
"{\"type\":\"simple\"," + "\"content\":\"i still love apples\"}," +
"{\"type\":\"simple\",\"content\":\"i love spam too\"}" +
"]" +
"}";
public static void main(final String... args)
throws IOException
{
final ObjectMapper mapper = new ObjectMapper();
final Complicated complicated
= mapper.readValue(JSONCONTENT, Complicated.class);
System.out.println("Deserialization done");
System.out.println("Serializing");
System.out.println(mapper.writeValueAsString(complicated));
}
}
class Complicated
{
private final String type;
private final List<Simple> params;
@JsonCreator
Complicated(@JsonProperty("type") final String type,
@JsonProperty("params") final List<Simple> params)
{
this.type = type;
this.params = new ArrayList<Simple>(params);
}
public String getType()
{
return type;
}
public List<Simple> getParams()
{
return Collections.unmodifiableList(params);
}
}
class Simple
{
private final String type;
private final String content;
@JsonCreator
Simple(@JsonProperty("type") final String type,
@JsonProperty("content") final String content)
{
this.type = type;
this.content = content;
}
public String getType()
{
return type;
}
public String getContent()
{
return content;
}
}
For your case you can use google gson : https://code.google.com/p/google-gson/ 对于您的情况,您可以使用google gson: https : //code.google.com/p/google-gson/
Using that library the following simple code produces the output you want : 使用该库,以下简单代码将生成所需的输出:
@Test
public void testGson() {
Gson gson = new Gson();
Param param = new Param("simple", "i still love apples");
Enclosure enclosure = new Enclosure("complex", param);
String json = gson.toJson(enclosure);
System.out.println(json);
}
output : {"type":"complex","param":{"type":"simple","content":"i still love apples"}}
You can also do more complex serializations using Gson so it should fit your needs as you expand in serialization. 您还可以使用Gson进行更复杂的序列化,因此随着您扩展序列化,它应该可以满足您的需求。
是的,这是可能的,我不知道Jackson的情况如何,但是在许多JSON库中,当Example类中有对象列表时,它就可以工作。
it's possible. 这是可能的。 Here is an example with flexjson: 这是flexjson的示例:
The relevant code sequence: 相关代码序列:
try ( ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream( baos ); FileOutputStream fos = new FileOutputStream( outputFileName ); ) { oos.writeObject( groupList ); fos.write( baos.toByteArray() ); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.