使用jackson将json转换为java对象的正确类型引用是什么

时间:2016-12-01 06:17:00

标签: java json jackson

我从api获得了以下json输出,

{"HYR":"[{\"LastUpdatedBy\":\"Bug 101510: VMukkanagoudar\",\"IATACountryCode\":\"US\",\"MetroCodeBool\":false,\"AirportName\":\"Sawyer County\",\"Latitude\":46,\"CityName\":\"Hayward\",\"MajorAirportBool\":false,\"Longitude\":-91,\"StatusCode\":\"A\",\"DisplayNameLocal\":\"Hayward, WI (HYR-Sawyer County)\",\"DisplayNameInternational\":\"Hayward, WI, United States (HYR-Sawyer County)\",\"UpdateDate\":\"2009-03-06 20:44:00.0\",\"AirportCode\":\"HYR\",\"AirportID\":5396808,\"RegionName\":\"Wisconsin\",\"IdenticalMetroCodeBool\":false,\"ExternalName\":\"HYR\",\"CountryCode\":\"USA\"}]",
 "VAA":"[{\"LastUpdatedBy\":\"fmoneo\",\"IATACountryCode\":\"FI\",\"MetroCodeBool\":false,\"AirportName\":\" \",\"Latitude\":63,\"CityName\":\"Vaasa\",\"MajorAirportBool\":true,\"Longitude\":22,\"StatusCode\":\"A\",\"DisplayNameLocal\":\"Vaasa (VAA)\",\"DisplayNameInternational\":\"Vaasa, Finland (VAA)\",\"UpdateDate\":\"2008-08-12 17:26:00.0\",\"AirportCode\":\"VAA\",\"AirportID\":4276566,\"RegionName\":\" \",\"IdenticalMetroCodeBool\":false,\"ExternalName\":\"VAA\",\"CountryCode\":\"FIN\"}]"
....(and so on)}

java对象如下

@JsonIgnoreProperties(ignoreUnknown = true)
public class Config {

  @JsonProperty("AirportCode")
  String airportCode;

  @JsonProperty("AirportID")
  Integer airportId;

  @JsonProperty("MetroCodeBool")
  Boolean metroCodeBool;

  @JsonProperty("MajorAirportBool")
  Boolean majorAirportBool;

  @JsonProperty("IdenticalMetroCodeBool")
  Boolean identicalMetroCodeBool;

  @JsonProperty("StatusCode")
  Character statusCode;

  //Getters and setters
}

转换代码是

String jsonStr = apiCall();

if(jsonStr != null)
{
   ObjectMapper mapper = new ObjectMapper();

   TypeReference<HashMap<String, ArrayList<Config>>> typeRef = new TypeReference<HashMap<String, ArrayList<Config>>>() {};

   HashMap<String, ArrayList<AirConfig>> configMap = mapper.readValue(jsonStr, typeRef);
 }

然而,我得到的错误是

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token
at [Source: java.io.StringReader@2b82d59e; line: 1, column: 2] (through reference chain: java.util.HashMap["HYR"])

我相信我创建的TypeReference是不正确的。我应该如何改变它的任何解决方案。

3 个答案:

答案 0 :(得分:0)

此JSON在您的情况下无效。见部分

{"HYR":"[{\"LastUpdatedBy\":\"Bug 101510:

有一个"不应该存在,所以有效部分就像:

{"HYR":[{\"LastUpdatedBy\":\"Bug 101510:

在您的情况下,您需要HYR的值作为数组,但它的值为String(由于引用)。删除引用确实会生成有效的JSON数组。

答案 1 :(得分:0)

在数组" "[的开头和结尾处,json中有额外的]"。你需要删除它们。

    try {
        String jsonStr = "{\"HYR\":[{\"LastUpdatedBy\":\"Bug 101510: VMukkanagoudar\",\"IATACountryCode\":\"US\",\"MetroCodeBool\":false,\"AirportName\":\"Sawyer County\",\"Latitude\":46,\"CityName\":\"Hayward\",\"MajorAirportBool\":false,\"Longitude\":-91,\"StatusCode\":\"A\",\"DisplayNameLocal\":\"Hayward, WI (HYR-Sawyer County)\",\"DisplayNameInternational\":\"Hayward, WI, United States (HYR-Sawyer County)\",\"UpdateDate\":\"2009-03-06 20:44:00.0\",\"AirportCode\":\"HYR\",\"AirportID\":5396808,\"RegionName\":\"Wisconsin\",\"IdenticalMetroCodeBool\":false,\"ExternalName\":\"HYR\",\"CountryCode\":\"USA\"}]}";
        if (jsonStr != null) {
            ObjectMapper mapper = new ObjectMapper();
            TypeReference<HashMap<String, ArrayList<Config>>> typeRef = new TypeReference<HashMap<String, ArrayList<Config>>>() {};
            HashMap<String, ArrayList<Config>> configMap = mapper.readValue(jsonStr, typeRef);
            ArrayList<Config> configList = configMap.get("HYR");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

答案 2 :(得分:0)

使用(Config使用正确的toString):

public static void main(String[] args) throws IOException {
    String jsonStr = "{\"HYR\":\"[{\\\"LastUpdatedBy\\\":\\\"Bug 101510: VMukkanagoudar\\\",\\\"IATACountryCode\\\":\\\"US\\\",\\\"MetroCodeBool\\\":false,\\\"AirportName\\\":\\\"Sawyer County\\\",\\\"Latitude\\\":46,\\\"CityName\\\":\\\"Hayward\\\",\\\"MajorAirportBool\\\":false,\\\"Longitude\\\":-91,\\\"StatusCode\\\":\\\"A\\\",\\\"DisplayNameLocal\\\":\\\"Hayward, WI (HYR-Sawyer County)\\\",\\\"DisplayNameInternational\\\":\\\"Hayward, WI, United States (HYR-Sawyer County)\\\",\\\"UpdateDate\\\":\\\"2009-03-06 20:44:00.0\\\",\\\"AirportCode\\\":\\\"HYR\\\",\\\"AirportID\\\":5396808,\\\"RegionName\\\":\\\"Wisconsin\\\",\\\"IdenticalMetroCodeBool\\\":false,\\\"ExternalName\\\":\\\"HYR\\\",\\\"CountryCode\\\":\\\"USA\\\"}]\"}";
    ObjectMapper mapper = new ObjectMapper();

    // step one: deserialize the map
    HashMap<String, String> configMap = mapper.readValue(jsonStr, new TypeReference<HashMap<String, String>>() {});

    // step two, deserialize the value for HYR
    final String hyr = configMap.get("HYR");
    List<Config> configs = mapper.readValue(hyr, new TypeReference<List<Config>>() {});

    System.out.println(configs);
}

在这里工作(杰克逊-2.3.2)和输出:

[Foo.Config(airportCode=HYR, airportId=5396808, metroCodeBool=false, majorAirportBool=false, identicalMetroCodeBool=false, statusCode=A)]