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);
}
With GSON