我无法从json获取字段

9jyewag0  于 2021-07-09  发布在  Java
关注(0)|答案(0)|浏览(269)

我有一个方法看起来像:

@RequestMapping(value = "/weather")
public void printWeather() throws JsonProcessingException {
    String url = mineApiURLwithKey;
    String json = restTemplate.getForObject(url, String.class);
    ObjectMapper mapper = new ObjectMapper();
    String temperature = mapper.readTree(json).get("sunset").asText();
}

json看起来像:

{
    "coord": {
        "lon": 19.7497,
        "lat": 53.5043
    },
    "weather": [
        {
            "id": 500,
            "main": "Rain",
            "description": "light rain",
            "icon": "10n"
        }
    ],
    "base": "stations",
    "main": {
        "temp": 280.67,
        "feels_like": 279.66,
        "temp_min": 279.82,
        "temp_max": 281.48,
        "pressure": 1010,
        "humidity": 72
    },
    "visibility": 10000,
    "wind": {
        "speed": 1.79,
        "deg": 325,
        "gust": 3.13
    },
    "rain": {
        "1h": 0.42
    },
    "clouds": {
        "all": 72
    },
    "dt": 1619039826,
    "sys": {
        "type": 3,
        "id": 2007860,
        "country": "PL",
        "sunrise": 1618975636,
        "sunset": 1619027504
    },
    "timezone": 7200,
    "id": 3093028,
    "name": "Lubawa",
    "cod": 200
}

但我也有个例外:
java.lang.nullpointerexception:无法调用“com.fasterxml.jackson.databind.jsonnode.astex()”,因为“com.fasterxml.jackson.databind.jsonnode.get(string)”的返回值为null

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题