【发布时间】:2016-03-29 12:32:41
【问题描述】:
我有这个代码(A):
JsonFileHandler<Device> jsonFileHandlerDevice;
final List<Device> devicesList = jsonFileHandlerDevice.getList();
和
class JsonFileHandler<T>:
@Override
public List<T> getList() {
List<T> t = null;
ObjectMapper mapper = new ObjectMapper();
if (!file.exists()) {
return null;
} else {
try {
t = mapper.readValue(file, new TypeReference<List<T>>(){});
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
return t;
}
这个代码(B):
@Override
public List<Device> getList() {
List<Device> t = null;
ObjectMapper mapper = new ObjectMapper();
if (!file.exists()) {
return null;
} else {
try {
t = mapper.readValue(file, new TypeReference<List<Device>>(){});
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
return t;
}
还有这个 json 文件:
[ {
"mobileOs" : "ios",
"osVersion" : 4.2,
"allocatedPort" : 0,
"hasSim" : false,
"uuid" : "uuid2",
"wazers" : [ {
"email" : null,
"emailPassword" : null,
"first" : null,
"last" : null,
"driverPhone" : null,
"riderPhone" : null,
"username" : null,
"password" : null,
"workEmail" : null,
"car" : null,
"model" : null,
"color" : null,
"plate" : null,
"obId" : null
} ],
"riders" : [ {
"email" : null,
"emailPassword" : null,
"first" : null,
"last" : null,
"driverPhone" : null,
"riderPhone" : null,
"username" : null,
"password" : null,
"workEmail" : null,
"car" : null,
"model" : null,
"color" : null,
"plate" : null,
"obId" : null
}, {
"email" : null,
"emailPassword" : null,
"first" : null,
"last" : null,
"driverPhone" : null,
"riderPhone" : null,
"username" : null,
"password" : null,
"workEmail" : null,
"car" : null,
"model" : null,
"color" : null,
"plate" : null,
"obId" : null
} ]
} ]
代码仅在执行代码时解析OK(B)
当运行代码 (A) 时,我们得到一个哈希映射而不是 Pojo Device。
【问题讨论】: