簡體   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