如何使用jackson解析这个JSON文件?

时间:2015-07-31 22:44:26

标签: java json jackson

我有这个JSON文件

{
  "personal": {
    "name": "John Doe",
    "age": "28",
    "gender": "male",
  }
}

这是我要解析的POJO

public class customerInfo{
   private String infoType;
   private Map<String, String>;
}

我想使用此POJO的原因是因为JSON文件也可能是

{
  "address": {
    "street": "123 main st",
    "state": "md",
    "zipcode": "21228"
  }
}

这是我尝试的但是没有用

customerInfo customer = mapper.readValue(new File("jsonTestFile.json"), customerInfo.class);

请帮忙吗?谢谢!

编辑:所以我希望infoType映射到个人或地址。而地图应该是,等等

2 个答案:

答案 0 :(得分:1)

您可以使用自定义反序列化器和序列化器完成此操作。

反序列化器看起来像

public class CustomerInfoSerializer extends JsonSerializer<CustomerInfo> {

    @Override
    public void serialize(CustomerInfo c, JsonGenerator jg, SerializerProvider sp) 
            throws IOException, JsonProcessingException {
        jg.writeStartObject();
        jg.writeObjectField(c.getInfoType(), c.getProperties());
        jg.writeEndObject();
    } 
}

Serializer只是

CustomerInfo

public class CustomerInfo { private String infoType; private final Map<String, String> properties = new HashMap<>(); public void addProperty(String name, String value) { properties.put(name, value); } public Map<String, String> getProperties() { return properties; } public String getInfoType() { return infoType; } public void setInfoType(String infoType) { this.infoType = infoType; } }

public static void main(String[] args) throws Exception {
    final String personal
            = "{"
            + "  \"personal\": {"
            + "    \"name\": \"John Doe\","
            + "    \"age\": \"28\","
            + "    \"gender\": \"male\""
            + "  }"
            + "}";
    final String address 
            = "{"
            + "  \"address\": {"
            + "    \"street\": \"123 main st\","
            + "    \"state\": \"md\","
            + "    \"zipcode\": \"21228\""
            + "  }"
            + "}";

    ObjectMapper mapper = new ObjectMapper();
    SimpleModule module = new SimpleModule();
    module.addDeserializer(
            CustomerInfo.class, 
            new CustomerInfoDeserializer("personal", "address")
    );
    module.addSerializer(CustomerInfo.class, new CustomerInfoSerializer());
    mapper.registerModule(module);

    CustomerInfo info = mapper.readValue(personal, CustomerInfo.class);
    System.out.println(mapper.writeValueAsString(info));

    info = mapper.readValue(address, CustomerInfo.class);
    System.out.println(mapper.writeValueAsString(info));
}

测试

<field label='Roles and Affiliations that May Retrieve Member List' type='list-multi' var='muc#roomconfig_getmemberlist'>

查看更多内容:

答案 1 :(得分:0)

如果您发布的两个json是唯一可能的,那么您的课程应该看起来像这样:

public class CustomerInfo {
    private Address address;
    private Personal personal;
}

你还应该创建这两个类:

public class Address {
    private String street;
    private String state;
    private String zipcode;
}

public class Personal {
    private String name;
    private String age;
    private String gender;
}

它应该有用,那么你只需要检查一个地址或个人是空的,这样你就可以知道要使用它了。

我希望你现在明白如何使用杰克逊图书馆。如果不清楚,请向我寻求帮助。