我已经尝试了所有方法来读取下面的JSON字符串,但仍然得到下面的错误。我的JSON字符串是有效的,我认为问题是子元素的Map有问题。调用的API URL:
https://api.weather.gov/gridpoints/OKX/33,35/forecast
下面是我的JSON字符串:
{
"@context": [
"https://geojson.org/geojson-ld/geojson-context.jsonld",
{
"@version": "1.1",
"wx": "https://api.weather.gov/ontology#",
"geo": "http://www.opengis.net/ont/geosparql#",
"unit": "http://codes.wmo.int/common/unit/",
"@vocab": "https://api.weather.gov/ontology#"
}
],
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-74.025095199999996,
40.727052399999998
],
[
-74.0295579,
40.705361699999997
],
[
-74.000948300000005,
40.701977499999998
],
[
-73.996479800000003,
40.723667899999995
],
[
-74.025095199999996,
40.727052399999998
]
]
]
},
"properties": {
"updated": "2023-06-01T07:20:53+00:00",
"units": "us",
"forecastGenerator": "BaselineForecastGenerator",
"generatedAt": "2023-06-01T08:08:29+00:00",
"updateTime": "2023-06-01T07:20:53+00:00",
"validTimes": "2023-06-01T01:00:00+00:00/P7DT11H",
"elevation": {
"unitCode": "wmoUnit:m",
"value": 2.1335999999999999
},
"periods": [
{
"number": 1,
"name": "Overnight",
"startTime": "2023-06-01T04:00:00-04:00",
"endTime": "2023-06-01T06:00:00-04:00",
"isDaytime": false,
"temperature": 59,
"temperatureUnit": "F",
"temperatureTrend": "rising",
"probabilityOfPrecipitation": {
"unitCode": "wmoUnit:percent",
"value": null
},
"dewpoint": {
"unitCode": "wmoUnit:degC",
"value": 13.333333333333334
},
"relativeHumidity": {
"unitCode": "wmoUnit:percent",
"value": 83
},
"windSpeed": "1 mph",
"windDirection": "NE",
"icon": "https://api.weather.gov/icons/land/night/fog?size=medium",
"shortForecast": "Patchy Fog",
"detailedForecast": "Patchy fog. Mostly cloudy. Low around 59, with temperatures rising to around 61 overnight. Northeast wind around 1 mph."
},
{
"number": 2,
"name": "Thursday",
"startTime": "2023-06-01T06:00:00-04:00",
"endTime": "2023-06-01T18:00:00-04:00",
"isDaytime": true,
"temperature": 78,
"temperatureUnit": "F",
"temperatureTrend": "falling",
"probabilityOfPrecipitation": {
"unitCode": "wmoUnit:percent",
"value": null
},
"dewpoint": {
"unitCode": "wmoUnit:degC",
"value": 13.888888888888889
},
"relativeHumidity": {
"unitCode": "wmoUnit:percent",
"value": 82
},
"windSpeed": "2 to 9 mph",
"windDirection": "E",
"icon": "https://api.weather.gov/icons/land/day/fog/smoke?size=medium",
"shortForecast": "Patchy Fog then Haze",
"detailedForecast": "Patchy fog before 8am, then haze and patchy smoke. Mostly sunny. High near 78, with temperatures falling to around 75 in the afternoon. East wind 2 to 9 mph."
},
{
"number": 3,
"name": "Thursday Night",
"startTime": "2023-06-01T18:00:00-04:00",
"endTime": "2023-06-02T06:00:00-04:00",
"isDaytime": false,
"temperature": 64,
"temperatureUnit": "F",
"temperatureTrend": "rising",
"probabilityOfPrecipitation": {
"unitCode": "wmoUnit:percent",
"value": null
},
"dewpoint": {
"unitCode": "wmoUnit:degC",
"value": 15
},
"relativeHumidity": {
"unitCode": "wmoUnit:percent",
"value": 75
},
"windSpeed": "5 to 9 mph",
"windDirection": "S",
"icon": "https://api.weather.gov/icons/land/night/few/fog?size=medium",
"shortForecast": "Mostly Clear then Patchy Fog",
"detailedForecast": "Patchy fog after 5am. Mostly clear. Low around 64, with temperatures rising to around 66 overnight. South wind 5 to 9 mph."
},
{
"number": 4,
"name": "Friday",
"startTime": "2023-06-02T06:00:00-04:00",
"endTime": "2023-06-02T18:00:00-04:00",
"isDaytime": true,
"temperature": 84,
"temperatureUnit": "F",
"temperatureTrend": "falling",
"probabilityOfPrecipitation": {
"unitCode": "wmoUnit:percent",
"value": 30
},
"dewpoint": {
"unitCode": "wmoUnit:degC",
"value": 15.555555555555555
},
"relativeHumidity": {
"unitCode": "wmoUnit:percent",
"value": 75
},
"windSpeed": "3 to 9 mph",
"windDirection": "S",
"icon": "https://api.weather.gov/icons/land/day/fog/tsra_hi,30?size=medium",
"shortForecast": "Patchy Fog then Chance Showers And Thunderstorms",
"detailedForecast": "Patchy fog before 8am, then a chance of showers and thunderstorms. Sunny. High near 84, with temperatures falling to around 80 in the afternoon. South wind 3 to 9 mph. Chance of precipitation is 30%."
},
{
"number": 5,
"name": "Friday Night",
"startTime": "2023-06-02T18:00:00-04:00",
"endTime": "2023-06-03T06:00:00-04:00",
"isDaytime": false,
"temperature": 62,
"temperatureUnit": "F",
"temperatureTrend": "rising",
"probabilityOfPrecipitation": {
"unitCode": "wmoUnit:percent",
"value": 50
},
"dewpoint": {
"unitCode": "wmoUnit:degC",
"value": 16.666666666666668
},
"relativeHumidity": {
"unitCode": "wmoUnit:percent",
"value": 81
},
"windSpeed": "8 mph",
"windDirection": "E",
"icon": "https://api.weather.gov/icons/land/night/tsra_hi,50?size=medium",
"shortForecast": "Chance Showers And Thunderstorms",
"detailedForecast": "A chance of showers and thunderstorms before 8pm, then a chance of showers and thunderstorms. Mostly cloudy. Low around 62, with temperatures rising to around 66 overnight. East wind around 8 mph. Chance of precipitation is 50%. New rainfall amounts less than a tenth of an inch possible."
},
{
"number": 6,
"name": "Saturday",
"startTime": "2023-06-03T06:00:00-04:00",
"endTime": "2023-06-03T18:00:00-04:00",
"isDaytime": true,
"temperature": 67,
"temperatureUnit": "F",
"temperatureTrend": "falling",
"probabilityOfPrecipitation": {
"unitCode": "wmoUnit:percent",
"value": 60
},
"dewpoint": {
"unitCode": "wmoUnit:degC",
"value": 15
},
"relativeHumidity": {
"unitCode": "wmoUnit:percent",
"value": 81
},
"windSpeed": "8 to 16 mph",
"windDirection": "NE",
"icon": "https://api.weather.gov/icons/land/day/tsra_sct,60?size=medium",
"shortForecast": "Showers And Thunderstorms Likely",
"detailedForecast": "Showers and thunderstorms likely. Mostly cloudy. High near 67, with temperatures falling to around 63 in the afternoon. Chance of precipitation is 60%. New rainfall amounts between a tenth and quarter of an inch possible."
},
{
"number": 7,
"name": "Saturday Night",
"startTime": "2023-06-03T18:00:00-04:00",
"endTime": "2023-06-04T06:00:00-04:00",
"isDaytime": false,
"temperature": 55,
"temperatureUnit": "F",
"temperatureTrend": null,
"probabilityOfPrecipitation": {
"unitCode": "wmoUnit:percent",
"value": 60
},
"dewpoint": {
"unitCode": "wmoUnit:degC",
"value": 10.555555555555555
},
"relativeHumidity": {
"unitCode": "wmoUnit:percent",
"value": 69
},
"windSpeed": "17 mph",
"windDirection": "NE",
"icon": "https://api.weather.gov/icons/land/night/rain_showers,60/rain_showers,50?size=medium",
"shortForecast": "Rain Showers Likely",
"detailedForecast": "Rain showers likely. Mostly cloudy, with a low around 55. Chance of precipitation is 60%."
},
{
"number": 8,
"name": "Sunday",
"startTime": "2023-06-04T06:00:00-04:00",
"endTime": "2023-06-04T18:00:00-04:00",
"isDaytime": true,
"temperature": 65,
"temperatureUnit": "F",
"temperatureTrend": null,
"probabilityOfPrecipitation": {
"unitCode": "wmoUnit:percent",
"value": 40
},
"dewpoint": {
"unitCode": "wmoUnit:degC",
"value": 8.3333333333333339
},
"relativeHumidity": {
"unitCode": "wmoUnit:percent",
"value": 69
},
"windSpeed": "9 to 14 mph",
"windDirection": "NE",
"icon": "https://api.weather.gov/icons/land/day/rain_showers,40?size=medium",
"shortForecast": "Chance Rain Showers",
"detailedForecast": "A chance of rain showers. Partly sunny, with a high near 65. Chance of precipitation is 40%."
},
{
"number": 9,
"name": "Sunday Night",
"startTime": "2023-06-04T18:00:00-04:00",
"endTime": "2023-06-05T06:00:00-04:00",
"isDaytime": false,
"temperature": 57,
"temperatureUnit": "F",
"temperatureTrend": null,
"probabilityOfPrecipitation": {
"unitCode": "wmoUnit:percent",
"value": 40
},
"dewpoint": {
"unitCode": "wmoUnit:degC",
"value": 9.4444444444444446
},
"relativeHumidity": {
"unitCode": "wmoUnit:percent",
"value": 72
},
"windSpeed": "12 mph",
"windDirection": "N",
"icon": "https://api.weather.gov/icons/land/night/rain_showers,40?size=medium",
"shortForecast": "Chance Rain Showers",
"detailedForecast": "A chance of rain showers. Mostly cloudy, with a low around 57. Chance of precipitation is 40%."
},
{
"number": 10,
"name": "Monday",
"startTime": "2023-06-05T06:00:00-04:00",
"endTime": "2023-06-05T18:00:00-04:00",
"isDaytime": true,
"temperature": 68,
"temperatureUnit": "F",
"temperatureTrend": null,
"probabilityOfPrecipitation": {
"unitCode": "wmoUnit:percent",
"value": 40
},
"dewpoint": {
"unitCode": "wmoUnit:degC",
"value": 10.555555555555555
},
"relativeHumidity": {
"unitCode": "wmoUnit:percent",
"value": 72
},
"windSpeed": "10 mph",
"windDirection": "N",
"icon": "https://api.weather.gov/icons/land/day/rain_showers,40/rain_showers,30?size=medium",
"shortForecast": "Chance Rain Showers",
"detailedForecast": "A chance of rain showers. Partly sunny, with a high near 68. Chance of precipitation is 40%."
},
{
"number": 11,
"name": "Monday Night",
"startTime": "2023-06-05T18:00:00-04:00",
"endTime": "2023-06-06T06:00:00-04:00",
"isDaytime": false,
"temperature": 60,
"temperatureUnit": "F",
"temperatureTrend": null,
"probabilityOfPrecipitation": {
"unitCode": "wmoUnit:percent",
"value": 40
},
"dewpoint": {
"unitCode": "wmoUnit:degC",
"value": 11.111111111111111
},
"relativeHumidity": {
"unitCode": "wmoUnit:percent",
"value": 72
},
"windSpeed": "9 mph",
"windDirection": "N",
"icon": "https://api.weather.gov/icons/land/night/rain_showers,40?size=medium",
"shortForecast": "Chance Rain Showers",
"detailedForecast": "A chance of rain showers. Mostly cloudy, with a low around 60. Chance of precipitation is 40%."
},
{
"number": 12,
"name": "Tuesday",
"startTime": "2023-06-06T06:00:00-04:00",
"endTime": "2023-06-06T18:00:00-04:00",
"isDaytime": true,
"temperature": 71,
"temperatureUnit": "F",
"temperatureTrend": null,
"probabilityOfPrecipitation": {
"unitCode": "wmoUnit:percent",
"value": 30
},
"dewpoint": {
"unitCode": "wmoUnit:degC",
"value": 11.111111111111111
},
"relativeHumidity": {
"unitCode": "wmoUnit:percent",
"value": 75
},
"windSpeed": "8 mph",
"windDirection": "N",
"icon": "https://api.weather.gov/icons/land/day/rain_showers,30/tsra_hi,30?size=medium",
"shortForecast": "Chance Rain Showers",
"detailedForecast": "A chance of rain showers before 2pm, then a chance of showers and thunderstorms. Mostly sunny, with a high near 71. Chance of precipitation is 30%."
},
{
"number": 13,
"name": "Tuesday Night",
"startTime": "2023-06-06T18:00:00-04:00",
"endTime": "2023-06-07T06:00:00-04:00",
"isDaytime": false,
"temperature": 60,
"temperatureUnit": "F",
"temperatureTrend": null,
"probabilityOfPrecipitation": {
"unitCode": "wmoUnit:percent",
"value": 30
},
"dewpoint": {
"unitCode": "wmoUnit:degC",
"value": 10.555555555555555
},
"relativeHumidity": {
"unitCode": "wmoUnit:percent",
"value": 65
},
"windSpeed": "8 mph",
"windDirection": "N",
"icon": "https://api.weather.gov/icons/land/night/tsra_hi,30/tsra_hi?size=medium",
"shortForecast": "Chance Showers And Thunderstorms",
"detailedForecast": "A chance of showers and thunderstorms. Partly cloudy, with a low around 60. Chance of precipitation is 30%."
},
{
"number": 14,
"name": "Wednesday",
"startTime": "2023-06-07T06:00:00-04:00",
"endTime": "2023-06-07T18:00:00-04:00",
"isDaytime": true,
"temperature": 72,
"temperatureUnit": "F",
"temperatureTrend": null,
"probabilityOfPrecipitation": {
"unitCode": "wmoUnit:percent",
"value": null
},
"dewpoint": {
"unitCode": "wmoUnit:degC",
"value": 9.4444444444444446
},
"relativeHumidity": {
"unitCode": "wmoUnit:percent",
"value": 67
},
"windSpeed": "9 mph",
"windDirection": "N",
"icon": "https://api.weather.gov/icons/land/day/rain_showers/tsra_hi?size=medium",
"shortForecast": "Slight Chance Rain Showers then Isolated Showers And Thunderstorms",
"detailedForecast": "A slight chance of rain showers before 2pm, then isolated showers and thunderstorms. Mostly sunny, with a high near 72."
}
]
}
}
我的Java POJO
@JsonIgnoreProperties
public class Forecast {
private int number;
private String name;
private String startTime;
private String endTime;
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStartTime() {
return startTime;
}
public void setStartTime(String startTime) {
this.startTime = startTime;
}
public String getEndTime() {
return endTime;
}
public void setEndTime(String endTime) {
this.endTime = endTime;
}
}
我的控制器
@Controller
@RequestMapping("/test")
public class ConsumeController {
RestTemplate restTemplate = new RestTemplate();
@RequestMapping("/show")
public ModelAndView showForecast() {
ModelAndView mv = new ModelAndView();
mv.setViewName("show");
ResponseEntity<Forecast[]> responseEntity = restTemplate.getForEntity("https://api.weather.gov/gridpoints/OKX/33,35/forecast", Forecast[].class);
Forecast[] responseBody = responseEntity.getBody();
List<Forecast> allForeCast = Arrays.asList(responseBody);
mv.addObject("myjson", allForeCast);
System.out.println("data: " + allForeCast);
return mv;
}
}
我的JSP:
<p>List data: </p>
<c:forEach items="${myjson}" var="myjson">
<td>${myjson.name}</td>
<td>${myjson.startTime}</td>
</c:forEach>
记录器错误:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `[Lcom.forecast.model.Forecast;` from Object value (token `JsonToken.START_OBJECT`)
at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 1, column: 1]
at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59) ~[jackson-databind-2.15.0.jar:2.15.0]
at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1752) ~[jackson-databind-2.15.0.jar:2.15.0]
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1526) ~[jackson-databind-2.15.0.jar:2.15.0]
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1473) ~[jackson-databind-2.15.0.jar:2.15.0]
如何解决他们的问题?许多感谢
1条答案
按热度按时间ovfsdjhp1#
你的问题是你的json有一个复杂的结构,你试图解析它的嵌套部分。如果我理解正确的话,你想反序列化“properties.periods”路径下的值,所以你应该改变你使用的方法。
1.手动遍历嵌套结构,到达所需的路径
1.使用Java对象的适当嵌套来重新生成响应结构,并反序列化为父对象(这里嵌套的'Properties'类只是为了简短,最好使用简单的类)
注意:在这两种情况下,您都应该注意到许多字段在所需的结果中不存在,因此请考虑适当的配置以忽略未知字段。
上你的POJO课程,或者