代码之家  ›  专栏  ›  技术社区  ›  Toseef Zafar

反序列化命名JSON数组[重复]

  •  -1
  • Toseef Zafar  · 技术社区  · 6 年前

    import java.io.IOException;
    import java.util.Arrays;
    import java.util.List;
    
    import org.codehaus.jackson.map.DeserializationConfig;
    import org.codehaus.jackson.map.ObjectMapper;
    import org.codehaus.jackson.map.annotate.JsonRootName;
    import org.junit.Assert;
    import org.junit.Test;
    
    public class RootNodeTest extends Assert {
    
        @JsonRootName("customers")
        public static class Customer {
            public String email;
        }
    
        @Test
        public void testUnwrapping() throws IOException {
            String json = "{\"customers\":[{\"email\":\"hello@world.com\"},{\"email\":\"john.doe@example.com\"}]}";
            ObjectMapper mapper = new ObjectMapper();
            mapper.configure(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true);
            List<Customer> customers = Arrays.asList(mapper.readValue(json, Customer[].class));
            System.out.println(customers);
        }
    }
    

    我一直在翻阅杰克逊的文档,这是我能弄明白的,但在运行它时,我得到以下错误:

    A org.codehaus.jackson.map.JsonMappingException has been caught, Root name 'customers' does not match expected ('Customer[]') for type [array type, component type: [simple type, class tests.RootNodeTest$Customer]] at [Source: java.io.StringReader@49921538; line: 1, column: 2]
    

    我希望在不创建包装器类的情况下完成这项工作。虽然这是一个示例,但我不想创建不必要的包装类,只用于展开根节点。

    0 回复  |  直到 10 年前
        1
  •  7
  •   araqnid    10 年前

    创建ObjectReader以显式配置根名称:

    @Test
    public void testUnwrapping() throws IOException {
        String json = "{\"customers\":[{\"email\":\"hello@world.com\"},{\"email\":\"john.doe@example.com\"}]}";
        ObjectReader objectReader = mapper.reader(new TypeReference<List<Customer>>() {})
                                          .withRootName("customers");
        List<Customer> customers = objectReader.readValue(json);
        assertThat(customers, contains(customer("hello@world.com"), customer("john.doe@example.com")));
    }
    

    (顺便说一句,这是杰克逊2.5,你有不同的版本吗?我有反序列化功能而不是DeserializationConfig.功能)

    @JsonRootName 注释。

    另外请注意,您可以直接请求 List<Customer> ObjectMapper.reader 就像第二个参数 ObjectMapper.readValue

        2
  •  -1
  •   Community CDub    8 年前

    根据 this @JsonRootName 注释只允许您展开包含pojo单个实例的json: "{\"customer\":{\"email\":\"hello@world.com\"}}";

        3
  •  -1
  •   Arthur Eirich    10 年前

    这个代码对我有用:

    import org.codehaus.jackson.map.ObjectMapper;
    import org.junit.Assert;
    import org.junit.Test;
    
    import java.io.IOException;
    import java.util.List;
    
    public class RootNodeTest extends Assert {
    
    public static class CustomerMapping {
        public List<Customer> customer;
    
        public List<Customer> getCustomer() {
            return customer;
        }
    
        public static class Customer {
            public String email;
    
            public String getEmail() {
                return email;
            }
        }
    
    }
    
    @Test
    public void testUnwrapping() throws IOException {
        String json = "{\"customer\":[{\"email\":\"hello@world.com\"},{\"email\":\"john.doe@example.com\"}]}";
        ObjectMapper mapper = new ObjectMapper();
        CustomerMapping customerMapping = mapper.readValue(json, CustomerMapping.class);
        List<CustomerMapping.Customer> customers = customerMapping.getCustomer();
        for (CustomerMapping.Customer customer : customers) {
            System.out.println(customer.getEmail());
        }
      }
    }