繁体   English   中英

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

[英]Is it possible do serialize/deserialize multilevel json objects?

当我有一个简单的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"
        }
    ]
}

如您所见,这个新Object的“ params”字段是一个json数组,并且该数组的每个元素都可以映射到我的Example pojo类。

有没有办法做到这一点? 抱歉,看似微不足道,但我找不到关于杰克逊的任何好的文档……它只是在谈论简单的案例。

干得好!

注意:类中没有设置器,这就是为什么我必须使用@JsonCreator 我的习惯,我不做豆子;)

如果您有针对不同字段的设置器,则完全不需要@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;
    }
}

对于您的情况,您可以使用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进行更复杂的序列化,因此随着您扩展序列化,它应该可以满足您的需求。

是的,这是可能的,我不知道Jackson的情况如何,但是在许多JSON库中,当Example类中有对象列表时,它就可以工作。

这是可能的。 这是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() ); } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM