Adr*_* H. 7 mapping json jackson
我从Web服务获取JSON并且不能影响JSON格式.下面的JSON代码只是一个例子来说明问题.该字段cars可以是包含Car对象的对象,也可以是空字符串.如果我可以更改Web服务,我会将空字符串更改为空对象,"cars" : {}而不是"cars" : "".
尝试将JSON映射到此Java对象时:
public class Person {
public int id;
public String name;
public Map<String, Car> cars;
}
Run Code Online (Sandbox Code Playgroud)
这有效:
{
"id" : "1234",
"name" : "John Doe",
"cars" : {
"Tesla Model S" : {
"color" : "silver",
"buying_date" : "2012-06-01"
},
"Toyota Yaris" : {
"color" : "blue",
"buying_date" : "2005-01-01"
}
}
}
Run Code Online (Sandbox Code Playgroud)
这失败了:
{
"id" : "1",
"name" : "The Dude",
"cars" : ""
}
Run Code Online (Sandbox Code Playgroud)
杰克逊处理这个案子的最佳方法是什么?如果有空字符串,我想去null场cars.我尝试过使用ACCEPT_EMPTY_STRING_AS_NULL_OBJECT,但没有用.
现场汽车可以包含汽车对象列表......这有效:
{
"id" : "1234",
"name" : "John Doe",
"cars" : {
"Tesla Model S" : {
"color" : "silver",
"buying_date" : "2012-06-01"
},
"Toyota Yaris" : {
"color" : "blue",
"buying_date" : "2005-01-01"
}
}
}
Run Code Online (Sandbox Code Playgroud)
"cars"元素值不是列表(aka数组).它是一个JSON对象,也可以被视为一个map类型集合,但它不是一个列表.
因此,为了重新解决这个问题,我们的目标是将有时候是对象的JSON反序列化,有时将空字符串反序列化为Java Map.
为了解决这个问题,我很惊讶ACCEPT_EMPTY_STRING_AS_NULL_OBJECT没有工作.我建议在http://jira.codehaus.org/browse/JACKSON上记录一个问题.
您可以实现自定义反序列化.以下是一个示例解决方案.如果目标数据结构具有其他Map引用,则需要相应地更改此解决方案.
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.ObjectCodec;
import org.codehaus.jackson.Version;
import org.codehaus.jackson.map.DeserializationContext;
import org.codehaus.jackson.map.JsonDeserializer;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.module.SimpleModule;
import org.codehaus.jackson.type.TypeReference;
public class Foo
{
public static void main(String[] args) throws Exception
{
SimpleModule module = new SimpleModule("CarsDeserializer", Version.unknownVersion());
module.addDeserializer(Map.class, new CarsDeserializer());
ObjectMapper mapper = new ObjectMapper().withModule(module);
Person person1 = mapper.readValue(new File("input1.json"), Person.class);
System.out.println(mapper.writeValueAsString(person1));
// {"id":1234,"name":"John Doe","cars":{"Tesla Model S":{"color":"silver","buying_date":"2012-06-01"},"Toyota Yaris":{"color":"blue","buying_date":"2005-01-01"}}}
Person person2 = mapper.readValue(new File("input2.json"), Person.class);
System.out.println(mapper.writeValueAsString(person2));
// {"id":1,"name":"The Dude","cars":{}}
}
}
class Person
{
public int id;
public String name;
public Map<String, Car> cars;
}
class Car
{
public String color;
public String buying_date;
}
class CarsDeserializer extends JsonDeserializer<Map<String, Car>>
{
@Override
public Map<String, Car> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException,
JsonProcessingException
{
ObjectCodec codec = jp.getCodec();
JsonNode node = codec.readTree(jp);
if (!"".equals(node.getTextValue()))
{
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(node, new TypeReference<Map<String, Car>>() {});
}
return new HashMap<String, Car>(); // or return null, if preferred
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4959 次 |
| 最近记录: |