1

JSON Response:

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

Model class:

class Account{

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

}

Since I have value attribute inside activeAccounts, deletedAccounts, holdAccounts, I am facing deserialization error at Account["activeAccounts"]. I just want activeAccount = 0, deletedAccounts = 1, holdAccounts = 3, and accountHolder = John as end result.

Thank you in advance.

1 Answers1

0

Firstly, the Json input provided here is incorrect in structure. Refer below:

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

Next, this json structure has got nested strucure, while the Java class you have created is flat and does not take care of the nested structure present.

You would need something like this:

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 + "]";
        }

    }
}

Each nested structure requires a specific class for it. eg. Data, Account_summary etc. All these are nested structures, and would require a specific class for it.

FYI..: I have used Jackson library to convert json string to java object.

Ankur
  • 892
  • 6
  • 11