java - Jackson 反序列化自定义对象列表

标签 java json jackson

我有以下 JSON,我正在尝试使用 Jackson API 对其进行反序列化

"attachments": {
            "file1": {
                "content": "",
                "name": "sample.json",
                "type": "application/json"
            },
            "file2": {
                "content": ""
                "name": "myspreadsheet.xlsx",
                "type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
            }
        },

我基本上需要一个 Attachment 类,它有一个 AttachmentFile 对象列表,如下所示:

public static AttachmentFile {
    String content;
    String name;
    String type;
}

如何使用自定义解串器实现此目的?

谢谢!

最佳答案

我使用jackson 1.9.12,序列化和反序列化HashMap没有问题。

附件:

import java.util.Map;

public class Attachments
{
   //@JsonDeserialize(as=HashMap.class) // use this if you want a HashMap
   public Map<String, AttachmentFile> attachments;
   public Attachments() {
   }
   public Attachments(
      final Map<String, AttachmentFile> attachments
   ) {
      this.attachments = attachments;
   }
}

附件:

public class AttachmentFile
{
   public String content;
   public String name;
   public String type;
   public AttachmentFile() {
   }
   public AttachmentFile(
      final String content,
      final String name,
      final String type
   ) {
      this.content = content;
      this.name = name;
      this.type = type;
   }
}

测试:

import java.util.HashMap;
import java.util.Map;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.junit.Assert;
import org.junit.Test;

public class AttachmentsTest
{
   @Test
   public void test()
   {
      try {

         final Map<String, AttachmentFile> attachments = new HashMap<String, AttachmentFile>();
         attachments.put(
            "file1",
            new AttachmentFile(
               "",
               "sample.json",
               "application/json"
            )
         );
         attachments.put(
            "file2",
            new AttachmentFile(
               "",
               "myspreadsheet.xlsx",
               "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
            )
         );
         final Attachments inputData = new Attachments();
         inputData.attachments = attachments;
         final ObjectMapper jsonMapper = new ObjectMapper();
         jsonMapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true);
         final String jsonString = jsonMapper.writeValueAsString(inputData);
         //System.out.println(jsonString);
         final Attachments outputData = jsonMapper.readValue(jsonString, inputData.getClass());
         Assert.assertNotNull(outputData);
         Assert.assertEquals(inputData.attachments.size(), outputData.attachments.size());
         Assert.assertEquals(inputData.attachments.get("file1").name, outputData.attachments.get("file1").name);
         Assert.assertEquals(inputData.attachments.get("file2").name, outputData.attachments.get("file2").name);

      } catch (final Exception e) {
         Assert.fail(e.getMessage());
      }
   }
}

关于java - Jackson 反序列化自定义对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18140418/

相关文章:

java - 如何在文件加载时保持 Java FXML Activity

java - 如果捕获空指针异常不是一个好习惯,那么捕获异常是一个好习惯吗?

java - 使用正则表达式操作 JSON 字符串

java - Elasticsearch 扩展内部文档数组

android - 使用 jackson 对象映射器将 JSON 数组转换为 POJO

java - 在Java+Spring中如何正确使用@PathVariable?

javascript - 等待ajax结果绑定(bind) knockout 模型

java - DropWizard Gradle 构建的问题

java - 当存在带有集合的属性时反序列化 JSON

java - 需要存储另一个领域模型的领域模型