是否可以对多级json对象进行序列化/反序列化?

时间:2014-03-18 18:09:28

标签: java jackson

当我有一个简单的json时很容易,请

{"type":"simple","content":"i love apples"}

我只是创造了一个pojo:

public class Example {
    public Object type;
    public Object content;
}

然后做:

ObjectMapper mapper = new ObjectMapper();
Example ex = mapper.readValue(getInputStream(),Example.class)

将完成这项工作。

但现在假设我有一些更复杂的东西,一个多层次的json

{
    "type": "complicated",
    "params": [
        {
            "type": "simple",
            "content": "i still love apples"
        },
        {
            "type": "simple", 
            "content":"i love spam too"
        }
    ]
}

你可以看到" params"这个新Object的字段是一个json数组,这个数组的每个元素都可以映射到我的Example pojo类。

有办法做到这一点吗?对不起,如果它看似微不足道,但我找不到关于杰克逊的任何好的文档...它只是谈论简单的案例。

4 个答案:

答案 0 :(得分:1)

你走了!

注意:类中没有setter,这就是我必须使用@JsonCreator的原因。我的习惯,我不做豆;)

如果你有不同字段的setter,你可以完全没有@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;
    }
}

答案 1 :(得分:0)

对于您的情况,您可以使用google gson:https://code.google.com/p/google-gson/

使用该库,以下简单代码可生成所需的输出:

@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"}}

您还可以使用Gson执行更复杂的序列化,以便在扩展序列化时满足您的需求。

答案 2 :(得分:-1)

是的,有可能,我不知道杰克逊是怎么回事,但在许多JSON库中,当你在Example类中有一个对象列表时,它可以工作。

答案 3 :(得分:-1)

这是可能的。这是flexjson的一个例子:

  1. 我有一个群组列表,其中每个群组都有一个用户列表
  2. 相关代码序列:

    try (
            ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream( baos );
            FileOutputStream fos = new FileOutputStream( outputFileName ); )
    {
        oos.writeObject( groupList );
        fos.write( baos.toByteArray() );
    }