J'essaie d'analyser l'objet suivant en utilisant jackson en Java.
[
[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]
]
J'obtiens l'erreur suivante.
Exception dans le thread "main" java.lang.IllegalArgumentException : Impossible de construire une instance de
com.beeline.DistanceMatrix
(bien qu'au moins un Créateur existe) : pas de constructeur d'argument de type String ni de méthode d'usine pour désérialiser à partir d'une valeur String ('[[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) à l'adresse com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:3668) at com.beeline.JsonParser.(JsonParser.java:17)
Les classes que j'utilise sont
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>>>();
}
J'ai vu plusieurs problèmes de ce type sur GitHub et SO. Mais je n'arrive pas à comprendre pourquoi cela ne fonctionne pas. Avez-vous une idée de ce qui ne va pas ?