Home > front end >  Unable to parse json into list of custom Java objects
Unable to parse json into list of custom Java objects

Time:06-30

I am reading JSON from a file, and when trying to unmarshal the file to a java object, I am not getting expected array of custom Java object, however, getting array of LinkedHashMap

Please see below objects

    public class Result<T>{
        private final Map<String, T> data = new LinkedHashMap<>();
    
         public Map<String, T> getAccounts(){
             return accounts;
         }
    }

JSON ->
{
   "data":{
     "account":[
           {
               "accountDetails":{
                      "accountId":"123",
                      "accountType":"Decon"
               }
           },
           {
               "accountDetails":{
                      "accountId":"890",
                      "accountType":"ACX"
               }
           },
           {
               "accountDetails":{
                      "accountId":"123",
                      "accountType":"OOOP"
               }
           }
     ]
   }
}


@Getter
@Setter
@ToString
public class Accounts{
   
   @Getter
   @Setter
   public static class AcountDetails{
      private String accountId;
      private String accountType;
   }
}    

I am trying to read this Json as below

 String accounts = Resource.asByteSource(Resources.getResource("account.json")).asCharSource(Charsets.UTF_8).read();
    ObjectMapper mapper = new ObjectMapper();
    Result<List<Accounts> finalResult = mapper.readValue(accounts, Result.class);



 In finalResult variable , 
    
    I am getting a map with key as "account" and value as list
    But, instead of List of "Accounts" object, I am getting list of **LinkedHashMap**

So bascially after parsing, Instead of getting array of Accounts objects, I am getting array of LinkedHashMap

Please find attached screenshot. Please advise

CodePudding user response:

If you are trying to get an Array of objects, your JSON would have to look something like this:

"element": [
{
  "element1": "Value 1",
  "element2": "Value 1"
},
{
  "element1": "Value 1",
  "element2": "Value 1"
}
]

When you are defining another object as you did:

"account":[
       {
           "accountDetails":{
                  "accountId":"123",
                  "accountType":"Decon"
           }
       },

You are creating a List of an Object that contains two more Objects. In Java that's a Map. List<Map<AccountId, AccountType>

Hope it helps

Edit for formatting purposes if "Element1" were another Object:

"element": [
{
  "element1": {
    "element3": "Value",
    "element4": "Value"
  },
  "element2": "Value 1"
},
{
  "element1": "Value 1",
  "element2": "Value 1"
}]

CodePudding user response:

First of all can you confirm what you want to do here:

Map final Map<String, T> accounts = new LinkedHashMap<>(); inside class Result? Because if it is a variable, then remove the leading Map and don't make the variable as final. Also you will need both setters and getters for the variable.

Secondly, your json has 'data' which has a account: [] pair, so first read value from data, then from account and you will get the list.

CodePudding user response:

It worked after changing Result<List finalResult = mapper.readValue(accounts, Result.class);

to

Result<List> finalResult = mapper.readValue(accounts, new TypeReference<Result<List>>() {});

  • Related