Read JSON Request and Map to JAVA POJO

76 views Asked by At

My Requirement: I have a JSON request in a table column which is like below.

{
"customerData": [{ "primaryData":[ {
"HNo": "8-10-2",
"APTNM": "SRSENCLAVE",
"STRT": "MGCLNY"
}],  
"officeData":{
 "ADDR": "1/7-25",
 "STRT": "FINDIST",
 "LM": "JBE"
 },
"ContactData": {
"PHNO":"XXXXXXXXX",
 "ZIP":"XXXXXX",
 "MAILCD": "XXXX"},
}
]}

I need to read it from DB and map the JSON values into three different class properties.i.e. PrimaryData.java. OfficeData.java,ContactData.java. I'm able to successfully read the request from DB but struck on how to map the values to properties in my three POJO classes. I tried using faster xml, google Gson, org.json but I could not get it well. Can someone give me an idea or part of code snippet? How I'm trying to achieve above (not sure if this approach is correct at all)

 List<Map<String, PrimaryData>> cxData = new ArrayList<Map<String,PrimaryData>>(); 
             JSONObject jSONObject = new JSONObject(query.getResultList().get(0).toString());             
                                 JSONArray jsonArray = jSONObject.getJSONArray("customerData");
                                 int length = jsonArray.length();
                                   for (int i=0; i<length; i++)
                                   {
                                       // FOR EACH ENTRY
                                       JSONObject OneEntry = jsonArray.getJSONObject(i);
                                       int OneEntrySize = OneEntry.length();
                                       JSONArray EntKey = OneEntry.names(); 
                                        Map<String, PrimaryData> map = new HashMap<String, PrimaryData>();
                                       for (int j=0; j<OneEntrySize;j++)
                                       {   // FOR EACH ITEM IN AN ENTRY
                                          String key = EntKey.getString(j);
                                           PrimaryData val = (PrimaryData)OneEntry.opt(key);;--unable to cast (can not cast JsonArray to PrimaryData)
                                           map.put(key, val);            
                                       }                       
                                       cxData.add(map);                 
                                   }    
1

There are 1 answers

0
Yogesh Narayanan On BEST ANSWER

With GSON

public class Data {
    @SerializedName("customerData") @Expose private List<CustomerData> customerData = null;
}

public class CustomerData {
    @SerializedName("primaryData") @Expose private List<PrimaryData> primaryData = null;
    @SerializedName("officeData") @Expose private OfficeData officeData;
    @SerializedName("ContactData") @Expose private ContactData contactData;
}

public class PrimaryData {
    @SerializedName("HNo") @Expose private String hNo;
    @SerializedName("APTNM") @Expose private String aPTNM;
    @SerializedName("STRT") @Expose private String sTRT;
}

public class OfficeData {
    @SerializedName("ADDR") @Expose private String aDDR;
    @SerializedName("STRT") @Expose private String sTRT;
    @SerializedName("LM") @Expose private String lM;
}

public class ContactData {
    @SerializedName("PHNO") @Expose private String pHNO;
    @SerializedName("ZIP") @Expose private String zIP;
    @SerializedName("MAILCD") @Expose private String mAILCD;
}


Gson gson = new Gson();
Data data = gson.fromJson(dataJSON, Data.class);