如果没有自定义序列化器,这个字符串的Jackson序列化是否可行

我想将我收到的JSON-String序列化为POJO,以便在我的代码中进一步使用,但是我很难在不编写自定义序列化程序的情况下使其工作。


我更喜欢在不编写自定义序列化程序的情况下作为解决方案,但如果这是唯一可行的方法,我将编写一个。


另外我相信我收到的数据是一个奇怪的JSON,因为我请求的列表不是作为列表使用[]而是作为使用的对象发送的{}。


我收到以下列表/对象(缩写):


{

    "results": {

        "ALL": {

            "currencyName": "Albanian Lek",

            "currencySymbol": "Lek",

            "id": "ALL"

        },

        "XCD": {

            "currencyName": "East Caribbean Dollar",

            "currencySymbol": "$",

            "id": "XCD"

        },

        "EUR": {

            "currencyName": "Euro",

            "currencySymbol": "â?¬",

            "id": "EUR"

        },

        "BBD": {

            "currencyName": "Barbadian Dollar",

            "currencySymbol": "$",

            "id": "BBD"

        },

        "BTN": {

            "currencyName": "Bhutanese Ngultrum",

            "id": "BTN"

        },

        "BND": {

            "currencyName": "Brunei Dollar",

            "currencySymbol": "$",

            "id": "BND"

        }

    }

}

哪个好。现在我写了另一个POJO作为数据的包装器,上面的层看起来像这样:


public class CurrencyListDTO implements Serializable {


  private List<Map<String, CurrencyDTO>> results;


  public CurrencyListDTO()

  {

  }


}

添加注释@JsonAnySetter或使用@JsonCreator也没有帮助,所以我再次删除它们,现在我想知道哪个小技巧可以启用正确的json序列化。


我的例外情况如下:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token

 at [Source: (String)"{"results":{"ALL":{"currencyName":"Albanian Lek","currencySymbol":"Lek","id":"ALL"},"XCD":{"currencyName":"East Caribbean Dollar","currencySymbol":"$","id":"XCD"},"EUR":{"currencyName":"Euro","currencySymbol":"â?¬","id":"EUR"},"BBD":{"currencyName":"Barbadian Dollar","currencySymbol":"$","id":"BBD"},"BTN":{"currencyName":"Bhutanese Ngultrum","id":"BTN"},"BND":{"currencyName":"Brunei Dollar","currencySymbol":"$","id":"BND"},"XAF":{"currencyName":"Central African CFA Franc","id":"XAF"},"CUP":{"cur"[truncated 10515 chars]; line: 1, column: 12] (through reference chain: com.nico.Banking.api.data.dto.CurrencyListDTO["results"])


12345678_0001
浏览 1109回答 2
2回答

BIG阳

你CurrencyListDTO应该看起来像下面。results属性是JSON Object应该直接映射到的属性Map。您可以将其转换为Collection使用keySet或values方法。class&nbsp;CurrencyListDTO&nbsp;implements&nbsp;Serializable&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;Map<String,&nbsp;CurrencyDTO>&nbsp;results; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;Map<String,&nbsp;CurrencyDTO>&nbsp;getResults()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;results; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;setResults(Map<String,&nbsp;CurrencyDTO>&nbsp;results)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.results&nbsp;=&nbsp;results; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;@Override &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;String&nbsp;toString()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;"CurrencyListDTO{"&nbsp;+ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"results="&nbsp;+&nbsp;results&nbsp;+ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'}'; &nbsp;&nbsp;&nbsp;&nbsp;}}

湖上湖

你应该CurrencyListDTO改为:public class CurrencyListDTO {&nbsp; &nbsp; private Map<String, CurrencyDTO> results;&nbsp; &nbsp; // getters and setters}因为results响应对象中的字段是另一个具有currencyIdas键且没有数组的对象。然后,您可以创建这样的货币列表:ObjectMapper mapper = new ObjectMapper();CurrencyListDTO result = mapper.readValue(json, CurrencyListDTO.class);List<CurrencyDTO> currencies = new ArrayList<>(result.getResults().values());
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java