如何将以下类型的复杂json响应映射到java模型类

时间:2021-06-13 04:15:29

标签: java json jsonparser jsonresult jsonresponse

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 作为最终结果。

提前致谢。

1 个答案:

答案 0 :(得分:0)

首先,这里提供的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 对象。

相关问题