阅读JSON请求并映射到JAVA POJO

时间:2016-12-19 06:19:05

标签: arrays json json-deserialization

我的要求: 我在表列中有一个JSON请求,如下所示。

{
"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"},
}
]}

我需要从DB中读取它并将JSON值映射到三个不同的类属性中。 PrimaryData.java。 OfficeData.java,ContactData.java。 我能够成功地从数据库中读取请求,但是如何将值映射到我的三个POJO类中的属性。我尝试使用更快的xml,谷歌Gson,org.json,但我不能很好。有人可以给我一个想法或代码片段的一部分吗? 我如何努力实现上述目标(不确定这种方法是否正确)

 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 个答案:

答案 0 :(得分:1)

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);