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

posted @ 2021-09-08 21:58  The-Chosen-One  阅读(1312)  评论(0编辑  收藏  举报