У меня сложный вложенный ответ JSON. Я хочу разобрать весь JSON. Я попробовал средство сопоставления объектов от Jackson и использовал автоматически сгенерированные классы POJO из http://www.jsonschema2pojo.org/. Но objectmapper не может отобразить JSON в java объект. Например, я хочу получить доступ к координатам.
{
"type": "FeatureCollection",
"features": [
{
"Feature": {
"name": "info",
"request-id": 18,
"computing-time (ms)": 44,
"number-of-points": 31,
"message": "Routing was successful, number of Points 31",
"description": ""
}
},
{
"type": "Feature",
"properties": {
"name": "startingpoint",
"_umap_options": {
"color": "DarkGreen"
}
},
"geometry": {
"type": "Point",
"coordinates": [
84.00761988067208,
28.213982682943577
]
}
},
{
"type": "Feature",
"properties": {
"name": "destination",
"_umap_options": {
"color": "DarkRed"
}
},
"geometry": {
"type": "Point",
"coordinates": [
84.00688545421255,
28.21633003390573
]
}
},
{
"type": "Feature",
"properties": {
"name": "Route",
"_umap_options": {
"opacity": "1"
}
},
"geometry": {
"type": "LineString",
"coordinates": [
[
84.00761988067208,
28.213982682943577
],
[
84.00683181003106,
28.21585748082081
],
[
84.00685453430238,
28.21587163692426
],
[
84.00686123982507,
28.21589994913116
],
[
84.00666156426061,
28.216708150879477
],
[
84.00672861948748,
28.216617253794162
],
[
84.00688545421255,
28.21633003390573
]
]
},
"details": {
"distance": [
[
0,
3,
70.82049008282796
],
[
3,
5,
106.922
],
[
5,
7,
42.881
],
[
7,
9,
46.08
],
[
9,
30,
378.041
]
]
},
"instructions": [
{
"distance": 70.82,
"heading": 51.99,
"sign": 0,
"interval": [
0,
3
],
"text": "Continue",
"time": 50990,
"street_name": ""
},
{
"distance": 106.922,
"sign": -2,
"interval": [
3,
5
],
"text": "Turn left onto Gaudako Mukh",
"time": 76983,
"street_name": "Gaudako Mukh"
},
{
"distance": 42.881,
"sign": 2,
"interval": [
5,
7
],
"text": "Turn right",
"time": 30874,
"street_name": ""
},
{
"distance": 424.121,
"sign": 7,
"interval": [
7,
30
],
"text": "Keep right onto Way to Matepani Monastry",
"time": 305366,
"street_name": "Way to Matepani Monastry"
},
{
"distance": 0,
"sign": 4,
"last_heading": 154.303263806682,
"interval": [
30,
30
],
"text": "Arrive at destination",
"time": 0,
"street_name": ""
}
]
},
{
"type": "Feature",
"properties": {
"_umap_options": {
"color": "DarkSlateGrey",
"fillOpacity": "0.2",
"fillColor": "Chocolate",
"opacity": "0.5"
},
"name": "barrierrange"
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
83.99725424061455,
28.22419972596586
]
]
]
}
}
]
}
Это мой код сопоставления.
package com.pojotest.jsonreader.controller;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.pojotest.jsonreader.model.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@RestController
public class Api
{
@PostMapping(value = "/feed")
public Object returnData(@RequestBody ReadResponse a) throws JsonProcessingException
{
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = a.getResponse();
try {
JsonNode features = jsonNode.get("features");
String Feature = features.toPrettyString(); //requets string
System.out.println(getFeature(Feature));
Feature featureObj = objectMapper.readValue(getFeature(Feature), Feature.class);
RefinedResponse refinedResponse = new RefinedResponse(a.getTimeStamp(), a.getRequest(), featureObj);
return refinedResponse;
}
catch (Exception e)
{
System.out.println(e.toString());
RefinedResponse refinedResponse = new RefinedResponse();
refinedResponse.setRequest(a.getRequest());
refinedResponse.setTimeStamp(a.getTimeStamp());
return refinedResponse;
}
}
public String getFeature(String wholeString)
{
int firstIndex = wholeString.indexOf("\"Feature\" : ") + (new String("\"Feature\" : ").length());
int lastIndex = wholeString.indexOf('}') + 1;
return wholeString.substring(firstIndex,lastIndex);
}
}
Я хочу разобрать весь этот JSON на Java объект. Но он выдает это предупреждение, и анализ не выполняется.
2020-06-18 15:24:21.475 WARN 21930 --- [nio-7777-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `java.lang.Float` out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.Float` out of START_ARRAY token
at [Source: (PushbackInputStream); line: 60, column: 13] (through reference chain: com.pojotest.jsonreader.fullmodel.Fullresponse["response"]->com.pojotest.jsonreader.fullmodel.Response["features"]->java.util.ArrayList[3]->com.pojotest.jsonreader.fullmodel.Feature["geometry"]->com.pojotest.jsonreader.fullmodel.Geometry["coordinates"]->java.util.ArrayList[0])]
Весь мой проект доступен здесь https://filebin.net/xk7qldtqre343oi9