简体   繁体   English

有可能对多级json对象进行序列化/反序列化吗?

[英]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的示例:

  1. I have a list of groups, in that every groups has a list of users 我有一个组列表,其中每个组都有一个用户列表
  2. 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.

相关问题 是否可以在C ++中序列化和反序列化对象? - Is it possible to Serialize and Deserialize objects in C++? 如何正确地将对象数组序列化和反序列化到/从json中? - How to properly serialize and deserialize an array of objects into/from json? 如何使用Jackson将Java Enums序列化和反序列化为JSON对象 - How to serialize and deserialize Java Enums as JSON objects with Jackson 是否可以将JSON序列化/反序列化为Java DTO,并将额外的字段放入映射中? - Is it possible to serialize/deserialize JSON to Java DTO with extra fields going into a map? 如何使用XStream在类型层次结构中序列化/反序列化对象? - How do I, using XStream, serialize/deserialize objects in a type hierarchy? 使用Groovy将bean序列化和反序列化为json - Serialize & Deserialize bean to json with Groovy Json使用相同的名称进行序列化和反序列化 - Json serialize and deserialize with same name 自定义序列化和反序列化以创建JSON - Custom serialize and deserialize to create JSON 在 Lucene 中索引多级 JSON 对象 - Indexing Multilevel JSON Objects in Lucene 不推荐使用GSON。 我们可以使用哪一个来对Java对象与JSON进行序列化和反序列化? - GSON is deprecated. Which one we can use to serialize and deserialize Java objects to and from JSON?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM