如何将值为JSON字符串的映射转换为对象?

时间:2019-08-08 12:28:25

标签: java json jackson

我正在将地图转换为对象,但是地图的某些值为Json String,我尝试使用杰克逊将其转换为下面的代码,但失败了。

public class Father {
    private String name;
    private List<Child> children;

}

public class Child {
    private String name;
}

public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
        String s= JSON.toJSONString(Arrays.asList(new Child("Bob"),new Child("Jackson")));
        Map<String,String> map=new HashMap();
        map.put("name","Jack")
        map.put("children",s);

// how to convert the map to a Father Object?
//this does not work
 ObjectMapper mapper = new ObjectMapper();
 mapper.convertValue(map, Father.class);

}

编辑:这是个例外:

Exception in thread "main" java.lang.IllegalArgumentException: Cannot deserialize instance of `java.util.ArrayList` out of VALUE_STRING token
 at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: model.Father["children"])
    at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:3750)
    at com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:3668)
    at service.BasicBehavior.main(BasicBehavior.java:25)
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList` out of VALUE_STRING token
 at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: model.Father["children"])
    at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63)
    at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1343)
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1139)
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1093)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.handleNonArray(CollectionDeserializer.java:332)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:265)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:245)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:27)
    at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:127)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:288)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
    at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:3745)
    ... 2 more

1 个答案:

答案 0 :(得分:0)

如果您使用customdeserializer序列化您的类,那就可以了。问题是您无法转换Child类。

public class CustomStringDeserializer extends JsonDeserializer<List<Child>> {

@Override
public List<Child> deserialize(JsonParser parser, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    List<Child> ret = new ArrayList<>();

    ObjectCodec codec = parser.getCodec();
    ObjectMapper mapper = new ObjectMapper();
    TreeNode node = codec.readTree(parser);

    if (node.isArray()){
        for (JsonNode n : (ArrayNode)node){
            ret.add(mapper.convertValue(n, Child.class));
        }
    } else if (node.isValueNode()){
        ret.add(mapper.convertValue(node, Child.class));
    }
    return ret;
   }
}

当然,您应该在Father类中将此反序列化器用作批注。

 @JsonDeserialize(using = CustomStringDeserializer.class)
 private List<Child> children;

此外,您应该这样使用Map<String, Object>

        Map<String,Object> map = new HashMap();
        map.put("name","Jack");
        map.put("children", Arrays.asList(new Child("Bob"),new Child("Jackson")));

所有这些更改之后,它将起作用。