1. 程式人生 > 其它 >JSONPObject在資料庫中的存取和遍歷屬性及value?

JSONPObject在資料庫中的存取和遍歷屬性及value?

第一、儲存的過程實現:
JSONObject jsonObjectbefore=new JSONObject();//新建JSONObject
jsonObjectbefore.put(varName,value);
String string =JSON.toJSONString(jsonObjectbefore) //=JSONObjet物件轉換為json字串
updateInformation.setcUpdateBefore(string);   將json字串設定為某一個類的物件的字元型屬性
iUpdateInformationService.insert(updateInformation); 將這個具體的類物件直接儲存在之前建立好的資料庫中。
第二,從資料庫中取出來過程的實現:
Map<String,Object> map = iUpdateInformationService.queryById(id);  直接到資料庫中取出不確定類的方式
if (map == null) {
result.error500("個人檔案修改資料不存在,請重新確認!");
} else {
//獲取到更新前的資料
for (String key : map.keySet()) {
System.out.println("key= " + key + " and value= " + map.get(key));
}
Object get1 =map.get("C_UPDATE_BEFORE");
String value1 =(String)get1;
System.out.println("C_UPDATE_BEFORE"+":"+value1);

第三、遍歷整個jsonobject物件獲得屬性和value值
//通過json字串轉化為JSONObject.並獲得其屬性Set列表
    JSONObject obj1 = JSON.parseObject(value1);
Set<String> updateproperty1 = obj1.keySet();
//將set轉換為列表list
List updatepropertylist=new ArrayList<>();
updatepropertylist.addAll(updateproperty1);
//遍歷set資料列表
for(String string:updateproperty1){
updatebeforelist.add(obj1.get(string)
); //遍歷取出屬性對照的value值
}
第四、就是map到具體類的轉換(需要其欄位一致):
UpdateBoth updateBoth=JSON.parseObject(JSON.toJSONString(map),UpdateBoth.class);
updateBoth.setCUpdateBefore(updatebeforelist);
updateBoth.setCUpdateNow(updatenowlist);
updateBoth.setCUpdateproperty(updatepropertylist);