【问题标题】:Getting and Mapping JSON inner List using RestTemplate of Spring使用 Spring 的 RestTemplate 获取和映射 JSON 内部列表
【发布时间】:2019-08-29 01:54:03
【问题描述】:

我需要映射一个 JSON 对象,但问题是它有一个内部自定义列表。在这种情况下如何使用 RestTemplate?

我正在尝试使用 ResponseEntityParameterizedTypeReference 但我还没有找到解决方案。

{
   "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":{
         "currencyName":"Cuban Peso",
         "currencySymbol":"$",
         "id":"CUP"
      },
      "USD":{
         "currencyName":"United States Dollar",
         "currencySymbol":"$",
         "id":"USD"
      }
   }
}

【问题讨论】:

    标签: json spring mapping custom-lists


    【解决方案1】:
    // you can create a custom class like below and try to map it
    
    class NodeWrapper{
      private Map<String, NodeData> results;
    }
    
    class NodeData{
       private String currencyName;
       private String currencySymbol;
       private id;
    }
    
    
    // also allow nulls using object mapper annotations
    

    【讨论】:

      猜你喜欢
      • 2017-02-26
      • 1970-01-01
      • 2018-04-26
      • 2021-05-18
      • 2019-07-06
      • 2015-05-07
      • 2018-05-16
      • 2014-11-13
      • 1970-01-01
      相关资源
      最近更新 更多