Java 如何将值为JSON字符串的映射转换为对象?
我正在将一个映射转换为对象,但是映射的一些值是Json字符串,我试图像下面的代码一样使用jackson进行转换,但失败了Java 如何将值为JSON字符串的映射转换为对象?,java,json,jackson,Java,Json,Jackson,我正在将一个映射转换为对象,但是映射的一些值是Json字符串,我试图像下面的代码一样使用jackson进行转换,但失败了 公共类父{ 私有字符串名称; 私人名单儿童; } 公营儿童{ 私有字符串名称; } 公共静态void main(字符串[]args)抛出InvocationTargetException、IllegalAccessException{ 字符串s=JSON.toJSONString(Arrays.asList(newchild(“Bob”)、newchild(“Jackson”
公共类父{
私有字符串名称;
私人名单儿童;
}
公营儿童{
私有字符串名称;
}
公共静态void main(字符串[]args)抛出InvocationTargetException、IllegalAccessException{
字符串s=JSON.toJSONString(Arrays.asList(newchild(“Bob”)、newchild(“Jackson”));
Map Map=newhashmap();
地图。放置(“姓名”、“杰克”)
地图放置(“儿童”,s);
//如何将贴图转换为父对象?
//这行不通
ObjectMapper mapper=新的ObjectMapper();
convertValue(映射,父类);
}
编辑:以下是例外情况:
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
如果您使用customdeserializer来序列化您的类,那么就可以了。问题是您无法转换
子类
类
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;
}
}
@JsonDeserialize(using = CustomStringDeserializer.class)
private List<Child> children;
此外,你应该像那样使用Map
Map<String,Object> map = new HashMap();
map.put("name","Jack");
map.put("children", Arrays.asList(new Child("Bob"),new Child("Jackson")));
Map Map=newhashmap();
地图。放置(“姓名”、“杰克”);
map.put(“children”)、array.asList(newchild(“Bob”)、newchild(“Jackson”));
在所有这些更改之后,它将工作。它工作吗?如果没有:你得到了什么,你期望什么?编辑:为什么要构建
地图
?这既不是JSON,也不是父JSON。为什么映射首先包含JSON?