如何反序列化与我的Java类同名的嵌套JSON对象

我正在尝试映射此JSON

{
"coord": {
    "lon": 26.94,"lat": 43.27
},"weather": [
    {
        "id": 802,"main": "Clouds","description": "scattered clouds","icon": "03d"
    }
],"base": "model","main": {
    "temp": 19.3,"pressure": 1012,"humidity": 66,"temp_min": 19.3,"temp_max": 19.3,"sea_level": 1012,"grnd_level": 971
},"wind": {
    "speed": 6.91,"deg": 182
},"clouds": {
    "all": 38
},"dt": 1573204594,"sys": {
    "country": "BG","sunrise": 1573188939,"sunset": 1573224978
},"timezone": 7200,"id": 727233,"name": "Shumen","cod": 200
}

到我自己创建的java对象

    package com.kosev.willitrain.model;
    import com.fasterxml.jackson.annotation.JsonAlias;
    import com.fasterxml.jackson.annotation.JsonProperty;
    import java.util.Map;

    public class Weather {
        @JsonProperty("name")
        private String cityName;
        private String type; // for weather type eg: Cloudy,Sunny,Raining etc...
        private float temp;
        private float tempMin;
        private float tempMax;
        private float windSpeed;
        private String icon;
        private float lon;
        private float lat;

        public Weather(){}

        @JsonProperty("main")
        private void unpackMain(Map<String,String> main) {
            temp = Float.parseFloat(main.get("temp"));
            tempMin = Float.parseFloat(main.get("temp_min"));
            tempMax = Float.parseFloat(main.get("temp_max"));
        }

        @JsonProperty("coord")
        private void unpackCoord(Map<String,String> coord) {
            lon = Float.parseFloat(coord.get("lon"));
            lat = Float.parseFloat(coord.get("lat"));
        }

        @JsonProperty("weather")
        private void unpackWeather(Map<String,String> weather) {
            type = weather.get("main");
            icon = weather.get("icon");
        }
    }

我得到的错误是这样的:

  

com.fasterxml.jackson.databind.exc.MismatchedInputException:无法从START_ARRAY令牌中反序列化java.util.LinkedHashMap<java.lang.Object,java.lang.Object>的实例    在[来源:(PushbackInputStream);行:1,列:46](通过参考链:com.kosev.willitrain.model.Weather [“ weather”])

snowcarangid 回答:如何反序列化与我的Java类同名的嵌套JSON对象

这是您的结构:

#!/bin/bash
OUT="GATE1"
COUNT="14"
DATA="CU4 CU2 CU9var CU3 CU1 CU11admin CU10opt not_sy CU_doonce cutttCU clocal_CU global_CU tivoli_CU"
while [[ -n $DATA ]]
do
    printf "%-20s %-5s %-20s \n" "${OUT}" "${COUNT}" "${DATA:0:48}"
    OUT=" "
    COUNT=" "
    DATA="${DATA:48}"
done

要读取rhis json,只需调用public class JsonClass { Coord coord; List<Weather> weather; String base; Main main; Wind wind; Clouds clouds; long dt; Sys sys; int timezone; int id; String name; int cod; private class Coord { private float lon; private float lat; } private class Weather { int id; String main; String description; String icon; } private class Main { float temp; int pressure; ... } private class Wind { float speed; int deg; } private class Clouds { int all; } private class Sys { String country; long sunrise; long sunset; } }

readValue

不要忘记添加ObjectMapper MAPPER = new ObjectMapper(); JsonClass jsnClz = MAPPER .readValue(json,JsonClass.class); / getter或使用龙目岛注释。

,
    package com.sample.demo;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class DemoApplicationTests {

    public static void main(String args[]) throws JsonParseException,JsonMappingException,IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        String response = "{\n" + "\"coord\": {\n" + "    \"lon\": 26.94,\n" + "    \"lat\": 43.27\n" + "},\n"
                + "\"weather\": [\n" + "    {\n" + "        \"id\": 802,\n" + "        \"main\": \"Clouds\",\n"
                + "        \"description\": \"scattered clouds\",\n" + "        \"icon\": \"03d\"\n" + "    }\n"
                + "],\n" + "\"base\": \"model\",\n" + "\"main\": {\n" + "    \"temp\": 19.3,\n"
                + "    \"pressure\": 1012,\n" + "    \"humidity\": 66,\n" + "    \"temp_min\": 19.3,\n"
                + "    \"temp_max\": 19.3,\n" + "    \"sea_level\": 1012,\n" + "    \"grnd_level\": 971\n" + "},\n"
                + "\"wind\": {\n" + "    \"speed\": 6.91,\n" + "    \"deg\": 182\n" + "},\n" + "\"clouds\": {\n"
                + "    \"all\": 38\n" + "},\n" + "\"dt\": 1573204594,\n" + "\"sys\": {\n" + "    \"country\": \"BG\",\n"
                + "    \"sunrise\": 1573188939,\n" + "    \"sunset\": 1573224978\n" + "},\n" + "\"timezone\": 7200,\n"
                + "\"id\": 727233,\n" + "\"name\": \"Shumen\",\n" + "\"cod\": 200\n" + "}";
        Weather weather = objectMapper.readValue(response,Weather.class);
    }
}

@JsonIgnoreProperties(ignoreUnknown = true)
class Weather {
    @JsonProperty("name")
    private String cityName;
    private String type; // for weather type eg: Cloudy,Sunny,Raining etc...
    private float temp;
    private float tempMin;
    private float tempMax;
    private float windSpeed;
    private String icon;
    private float lon;
    private float lat;

    @JsonIgnoreProperties("base")
    public Weather() {
    }

    @JsonProperty("main")
    private void unpackMain(Map<String,String> main) {
        temp = Float.parseFloat(main.get("temp"));
        tempMin = Float.parseFloat(main.get("temp_min"));
        tempMax = Float.parseFloat(main.get("temp_max"));
    }

    @JsonProperty("coord")
    private void unpackCoord(Map<String,String> coord) {
        lon = Float.parseFloat(coord.get("lon"));
        lat = Float.parseFloat(coord.get("lat"));
    }

    @JsonProperty("weather")
    private void unpackWeather(List<Map<String,String>> weather) {
        System.out.println(weather);
        type = weather.get(0).get("main"); // Taken first element,change as per your requirement
        icon = weather.get(0).get("icon");
    }
}

天气应为map列表,而不是Map。

本文链接:https://www.f2er.com/3138717.html

大家都在问