【问题标题】:How to map the complex json response of the following type to java model class如何将以下类型的复杂json响应映射到java模型类
【发布时间】:2021-08-29 10:44:48
【问题描述】:

JSON 响应:

{
"data": {
     "account_summary": [
      {
        "aggregation":{
           "activeAccounts: {
               "value": "0"
            },
            "deletedAccounts: {
               "value": "1"
            },
            "holdAccounts: {
               "value": "3"
            }
          },
          "accountHolder": "John"
}

模型类:

class Account{

  private String activeAccounts;
  private String deletedAccounts;
  private String holdAccounts;
  private String accountHolder;

}

由于我在 activeAccounts、deletedAccounts、holdAccounts 中有 value 属性,我在 Account["activeAccounts"] 处面临反序列化错误。我只想要 activeAccount = 0、deletedAccounts = 1、holdAccounts = 3 和 accountHolder = John 作为最终结果。

提前谢谢你。

【问题讨论】:

    标签: java json jsonparser jsonresult jsonresponse


    【解决方案1】:

    首先,这里提供的 Json 输入结构不正确。参考如下:

    {
       "data":{
          "account_summary":[
             {
                "aggregation":{
                   "activeAccounts":{
                      "value":"0"
                   },
                   "deletedAccounts":{
                      "value":"1"
                   },
                   "holdAccounts":{
                      "value":"3"
                   }
                },
                "accountHolder":"John"
             }
          ]
       }
    }
    

    接下来,这个 json 结构有嵌套结构,而你创建的 Java 类是扁平的,不处理存在的嵌套结构。

    你需要这样的东西:

    import java.util.List;
    
    import com.fasterxml.jackson.annotation.JsonProperty;
    import com.fasterxml.jackson.core.JsonProcessingException;
    import com.fasterxml.jackson.databind.JsonMappingException;
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    public class JsonTester {
    
        public static void main(String[] args) throws JsonMappingException, JsonProcessingException {
    
            String json = "{\"data\":{\"account_summary\":[{\"aggregation\":{\"activeAccounts\":{\"value\":\"0\"},\"deletedAccounts\":{\"value\":\"1\"},\"holdAccounts\":{\"value\":\"3\"}},\"accountHolder\":\"John\"}]}}";
    
            ObjectMapper m = new ObjectMapper();
            AccountInfo a = m.readValue(json, AccountInfo.class);
            System.out.println(a);
        }
    
        static class AccountInfo {
    
            @JsonProperty("data")
            Data data;
    
            @Override
            public String toString() {
                return "AccountInfo [data=" + data + "]";
            }
        }
        
        static class Data {
            @JsonProperty("account_summary")
            List<AccountSumamry> accountSummary;
    
            @Override
            public String toString() {
                return "Data [accountSummary=" + accountSummary + "]";
            }
    
        }
    
        static class AccountSumamry {
            @JsonProperty("aggregation")
            Aggregation aggregation;
            @JsonProperty("accountHolder")
            String accountHolder;
    
            @Override
            public String toString() {
                return "AccountSumamry [aggregation=" + aggregation + ", accountHolder=" + accountHolder + "]";
            }
    
        }
    
        static class Aggregation {
            @JsonProperty("activeAccounts")
            Account activeAccounts;
            @JsonProperty("deletedAccounts")
            Account deletedAccounts;
            @JsonProperty("holdAccounts")
            Account holdAccounts;
    
            @Override
            public String toString() {
                return "Aggregation [activeAccounts=" + activeAccounts + ", deletedAccounts=" + deletedAccounts
                        + ", holdAccounts=" + holdAccounts + "]";
            }
    
        }
    
        static class Account {
            @JsonProperty("value")
            String value;
    
            @Override
            public String toString() {
                return "Account [value=" + value + "]";
            }
    
        }
    }
    

    每个嵌套结构都需要一个特定的类。例如。 Data、Account_summary 等。所有这些都是嵌套结构,需要一个特定的类。

    仅供参考..:我使用 Jackson 库将 json 字符串转换为 java 对象。

    【讨论】:

    • 非常感谢,Ankur。对不起结构。我会尝试实现这一点,看看。
    • 让我知道这是否有效。乐于助人!!
    猜你喜欢
    • 2018-06-19
    • 1970-01-01
    • 2018-02-06
    • 1970-01-01
    • 2020-09-13
    • 1970-01-01
    • 2016-03-19
    • 2018-11-07
    • 1970-01-01
    相关资源
    最近更新 更多