简体   繁体   English

使用Jackson将json转换为Java对象的正确类型参考是什么

[英]What is correct type reference for converting the json into java object using jackson

I have the following json output that I get from an api, 我有以下从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)}

The java object is as follows 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
}

The code for conversion is 转换的代码是

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);
 }

However, the error I get is 但是,我得到的错误是

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"])

I believe the TypeReference I have created is incorrect. 我相信我创建的TypeReference不正确。 Any solutions to how I should change it. 任何有关如何更改它的解决方案。

This JSON is invalid in your case. 在您的情况下,此JSON无效。 See the part 看部分

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

There is a " that should not be there, so valid part would be like: 有一种"不应该在那里,所以有效的部分是这样的:

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

In your case, you are require a value of HYR to be an array, but it is of String value (due to quotation). 在您的情况下,您需要将HYR的值设置为数组,但该值必须为String值(由于引用)。 Removing quotation will indeed make valid JSON array. 删除引号确实会产生有效的JSON数组。

There are extra " in json at the start and end of Array "[ ]" . You need to remove them. 数组"[ ]"开头和结尾处的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();
    }
}

using (with your Config with a proper toString ): 使用(与您的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);
}

Works here (jackson-2.3.2) and outputs: 在这里工作(杰克逊2.3.2)并输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM