gpt4 book ai didi

java - Json 到 Java 对象的转换 - 在 Jackson 中失败

转载 作者:行者123 更新时间:2023-11-30 07:42:32 25 4
gpt4 key购买 nike

我正在尝试使用 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]
]

我是否收到以下错误。

Exception in thread "main" java.lang.IllegalArgumentException: Cannot construct instance of com.beeline.DistanceMatrix (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('[[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]]') at [Source: UNKNOWN; line: -1, column: -1] at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:3750) at com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:3668) at 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 上看到了多个类似的问题。但我不明白为什么它不起作用。有什么地方出了问题吗?

最佳答案

你的 DistanceMatrix类描述了一个 JSON 对象,它有一个名为 matrix 的键和类型的相应值 ArrayList<ArrayList<Map<String, Float>>> .

您需要将 JSON 包装成类似于 { "matrix": <your actual JSON> } 的形式或将类型指定为 convertValue直接。

关于java - Json 到 Java 对象的转换 - 在 Jackson 中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54482043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com