Partially deserialize json into POJO but keep some raw json as field

Viewed 433

I have a json as follows:

"customer": {  
     "personal": {  
        "name": “jim johnson”,  
        “miscellaneous”: {  
            “active”: “true”,  
            “addons”: {  
              "location": “us”  
            },  
        “customer_id”: “1234”   
        }    
    },  
    “source”: “main db”  
}  

I can create an simple POJO mapping everything one to one but what I would like is the following:
I would like to have an object such as follows:

class Customer {  
   Personal personal;  
   String source;  
}   

class Personal {  
    String name;
    String customer_id;  
    String miscellaneous; // <—— This is the problem  
    // JsonObject miscellaneous;
}    

How can I deserialize the json and have the miscellaneous kept as a string of raw json string? Or even have it as a raw json element?

3 Answers
public class myClassName {
        public Customer customer;
        public class Customer {
            public Personal personal;
            public String source;
        }
        public class Personal {
            public String name;
           public Miscellaneous miscellaneous;
        }
        public class Miscellaneous {
            public String active;
           public Addons addons;
                   public String customerId;
        }
        public class Addons {
            public String location;
        }
    }

Because, as per your JSON miscellaneous isn't plain String.

    “miscellaneous”: {  
        “active”: “true”,  
        “addons”: {  
          "location": “us”  
        },  
    “customer_id”: “1234”   
    } 

You need to write a class and declare required property for miscellaneous

   public class Miscellaneous {
       private boolean active;
       private Addons addons;
       private String customerId;
     // getter and setter
    }

And, then

String miscellaneous;

should be change to

Miscellaneous miscellaneous;
Related