我正在尝试使用Java中的jackson解析以下对象。
[
[null, {
"distance": 2272,
"duration": 580
}, {
"distance": 7217,
"duration": 1393
}, {
"distance": 5773,
"duration": 1061
}, {
"distance": 6208,
"duration": 1170
}, {
"distance": 18846,
"duration": 2686
}],
[{
"distance": 2262,
"duration": 558
}, null, {
"distance": 7144,
"duration": 1366
}, {
"distance": 4803,
"duration": 801
}, {
"distance": 5238,
"duration": 910
}, {
"distance": 17876,
"duration": 2426
}],
[{
"distance": 7207,
"duration": 1393
}, {
"distance": 7505,
"duration": 1411
}, null, {
"distance": 12375,
"duration": 2010
}, {
"distance": 12809,
"duration": 2119
}, {
"distance": 24868,
"duration": 3462
}],
[{
"distance": 7481,
"duration": 1255
}, {
"distance": 4643,
"duration": 831
}, {
"distance": 11497,
"duration": 2060
}, null, {
"distance": 434,
"duration": 109
}, {
"distance": 13197,
"duration": 1657
}],
[{
"distance": 7723,
"duration": 1326
}, {
"distance": 4884,
"duration": 902
}, {
"distance": 11739,
"duration": 2130
}, {
"distance": 434,
"duration": 107
}, null, {
"distance": 12999,
"duration": 1628
}],
[{
"distance": 20372,
"duration": 2918
}, {
"distance": 17534,
"duration": 2493
}, {
"distance": 25912,
"duration": 3432
}, {
"distance": 13669,
"duration": 1774
}, {
"distance": 13414,
"duration": 1779
}, null]
]
我遇到以下错误。
线程“主”中的异常java.lang.IllegalArgumentException:无法 构造
com.beeline.DistanceMatrix
的实例(尽管至少 存在一个创建者):不存在用于以下操作的字符串参数构造函数/工厂方法 从字符串值反序列化 ('[[null,{“ distance”:2272,“ duration”:580},{“ distance”:7217,“ duration”:1393},{“ distance”:5773,“ duration”:1061},{“距离”:6208,“持续时间”:1170},{“距离”:18846,“持续时间”:2686}],[{“距离”:2262,“持续时间”:558},空,{“距离”:7144 ,“持续时间”:1366},{“距离”:4803,“持续时间”:801},{“距离”:5238,“持续时间”:910},{“距离”:17876,“持续时间”:2426}] ,[{“ distance”:7207,“ duration”:1393},{“ distance”:7505,“ duration”:1411},null,{“ distance”:12375,“ duration”:2010},{“ distance” :12809,“持续时间”:2119},{“距离”:24868,“持续时间”:3462}],[{“距离”:7481,“持续时间”:1255},{“距离”:4643,“持续时间” :831},{“ distance”:11497,“ duration”:2060},null,{“ distance”:434,“ duration”:109},{“ distance”:13197,“ duration”:1657}],[ {“距离”:7723,“持续时间”:1326},{“距离”:4884,“持续时间”:902},{“距离”:11739,“持续时间”:2130},{“距离”:434,” duration“:107},null,{” distance“:12999,” duration“:1628}],[{” distance“:20372,” duration“:2918},{” distance“:17534,” duration“:2493 },{“ distance”:25912,“ duration”:3432},{“ dista nce“:13669,” duration“:1774},{” distance“:13414,” duration“:1779},null]]') 在[资料来源:未知;行:-1,列:-1]在 com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:3750) 在 com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:3668) 在com.beeline.JsonParser。(JsonParser.java:17)
我正在使用的类是
JsonParser.java
public class JsonParser {
private DistanceMatrix matrix = new DistanceMatrix();
public JsonParser() {
String jsonString = "[[null,{\"distance\":2272,\"duration\":580},{\"distance\":7217,\"duration\":1393},{\"distance\":5773,\"duration\":1061},{\"distance\":6208,\"duration\":1170},{\"distance\":18846,\"duration\":2686}],[{\"distance\":2262,\"duration\":558},null,{\"distance\":7144,\"duration\":1366},{\"distance\":4803,\"duration\":801},{\"distance\":5238,\"duration\":910},{\"distance\":17876,\"duration\":2426}],[{\"distance\":7207,\"duration\":1393},{\"distance\":7505,\"duration\":1411},null,{\"distance\":12375,\"duration\":2010},{\"distance\":12809,\"duration\":2119},{\"distance\":24868,\"duration\":3462}],[{\"distance\":7481,\"duration\":1255},{\"distance\":4643,\"duration\":831},{\"distance\":11497,\"duration\":2060},null,{\"distance\":434,\"duration\":109},{\"distance\":13197,\"duration\":1657}],[{\"distance\":7723,\"duration\":1326},{\"distance\":4884,\"duration\":902},{\"distance\":11739,\"duration\":2130},{\"distance\":434,\"duration\":107},null,{\"distance\":12999,\"duration\":1628}],[{\"distance\":20372,\"duration\":2918},{\"distance\":17534,\"duration\":2493},{\"distance\":25912,\"duration\":3432},{\"distance\":13669,\"duration\":1774},{\"distance\":13414,\"duration\":1779},null]]";
ObjectMapper oMapper = new ObjectMapper();
oMapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
oMapper.enable(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT);
matrix = oMapper.convertValue(jsonString, DistanceMatrix.class);
System.out.print(matrix);
}
}
DistanceMatrix.java
public class DistanceMatrix {
public ArrayList<ArrayList<Map<String, Float>>> matrix = new ArrayList<ArrayList<Map<String, Float>>>();
}
我在GitHub和SO上看到了多个类似的问题。但是我不知道为什么它不起作用。关于出什么问题的任何指示?
答案 0 :(得分:4)
您的DistanceMatrix
类描述了一个JSON对象,该对象具有一个名为matrix
的键和一个类型为ArrayList<ArrayList<Map<String, Float>>>
的对应值。
您需要包装JSON以使其看起来像{ "matrix": <your actual JSON> }
或直接将类型指定为convertValue
。
答案 1 :(得分:0)
对我有用的实际答案都来自@dnt给出的答案以及Google小组中的相同问题以及一些自己的实验。
matrix = oMapper.convertValue(jsonString, DistanceMatrix.class);
应更改为
try {
matrix = oMapper.readValue(jsonString, DistanceMatrix.class);
}
因此,这将正确读取JSON,并尝试将其与给定的结构进行匹配。
无法反序列化
com.beeline.DistanceMatrix
的实例 START_ARRAY令牌
原因是距离矩阵类描述了JSON对象。输入预期{{“ matrix”:}
对于上述课程,这是可以接受的
[{"distance":7217,"duration":null},{"distance":null,"duration":1061}]
这是不可接受的
[{"distance":7217,"duration":1393},null]
我很确定您可以用Java处理不同的内容(我是初学者)。但是我用空对象符号 {} 替换了所有空值。因此,空对象将创建没有键值对的地图。我将研究如何在 Map 结构中存储null。或者,如果有人对此发表评论,我将相应地更新答案。
感谢贡献者。