JSON龐雜數據處置之Json樹形構造數據轉Java對象並存儲到數據庫的完成。本站提示廣大學習愛好者:(JSON龐雜數據處置之Json樹形構造數據轉Java對象並存儲到數據庫的完成)文章只能為提供參考,不一定能成為您想要的結果。以下是JSON龐雜數據處置之Json樹形構造數據轉Java對象並存儲到數據庫的完成正文
在網站開辟中常常碰到級聯數據的展現,好比選擇城市的時刻彈出的省市縣選擇界面。許多前端制造人員習氣於從JSON中而不是從數據庫中獲得省市縣數據。那末在選擇了省市縣中的某一個城市 ,存儲到數據庫中須要存儲所選城市的代碼。所以須要一個能將JSON數據(普通存儲在javascript劇本中)構造全體導入到數據庫中的功效。
JSON的特色是支撐層級構造、支撐數組表現的對象 。上面的示例引見若何將JSON的省市縣數據保留到數據庫中,完成道理異常簡略,就是應用JSON的java對象包API,將條理構造的JSON對象數組經由過程遞歸的方法首選轉換為Java對象數組,然後保留到數據庫中。
完成步調是:
(一)起首界說一個JsonItem實體類:
package org.openjweb.core.entity; public class JsonItem { private String sub_id; private String sub_name; private JsonItem[] items; public JsonItem[] getItems() { return items; } public void setItems(JsonItem[] items) { this.items = items; } public String getSub_id() { return sub_id; } public void setSub_id(String sub_id) { this.sub_id = sub_id; } public String getSub_name() { return sub_name; } public void setSub_name(String sub_name) { this.sub_name = sub_name; } }
(二)界說一個對象類,在對象類中讀取Json數據文件,並停止遞歸挪用 :
public static String importJson(String fullFileName,String jsonType,String encoding,HttpServletRequest request) throws Exception { //Json轉換為實體類參考:http://www.cnblogs.com/hoojo/archive/2011/04/21/2023805.html String sReturn = ""; String jsonData = ""; try { jsonData = FileUtil.getTextFileContent(fullFileName, encoding); } catch(Exception ex) { sReturn ="讀Json文件掉敗!"; } //獲得rootId //logger.info(""); jsonData = jsonData.replace("\"items\":\"\"", ""); //去失落空的 items String parentId = jsonData.substring(jsonData.indexOf("\"id\":")+5); parentId = parentId.substring(0,parentId.indexOf(",")).trim(); parentId = parentId.replace("\"", ""); logger.info("root id=="+parentId); String parentName = jsonData.substring(jsonData.indexOf("\"name\":")+7); parentName = parentName.substring(0,parentName.indexOf(",")).trim(); parentName = parentName.replace("\"", ""); logger.info("root Name=="+parentName); String rootData = jsonData.substring(jsonData.indexOf("\"items\":")+8,jsonData.lastIndexOf("}")); rootData = jsonData.substring(jsonData.indexOf("[")+1,jsonData.lastIndexOf("]")); //分歧json的id,name表現紛歧樣,同一換成sub_id,subname以便與JsonItem的類屬性婚配 // 調換前方便同一處置 rootData = rootData.replace("city_id", "sub_id"); rootData = rootData.replace("sub_city", "sub_name"); rootData = rootData.replace("city", "sub_name"); rootData = rootData.replace("sub_txt", "sub_name"); rootData = rootData.replace("sub_industry", "sub_name"); rootData = rootData.replace("industry_id", "sub_id"); rootData = rootData.replace("industry", "sub_name"); rootData = rootData.replace("sub_profession", "sub_name"); rootData = rootData.replace("profession_id", "sub_id"); rootData = rootData.replace("profession", "sub_name"); //將rootData轉換為array rootData = "[" + rootData + "]"; try { FileUtil.str2file(rootData, "d:/jsonData.txt", "utf-8");//存儲到磁盤檢討字符串轉換能否准確 } catch(Exception ex) { } JSONArray jsonArray = JSONArray.fromObject(rootData); Object[] os = jsonArray.toArray(); JsonItem[] items = (JsonItem[]) JSONArray.toArray(jsonArray, JsonItem.class); saveJsonEnt(jsonType,parentId,parentName,"-1",new Long(1)); dealJson(items,parentId,jsonType,new Long(1)); return sReturn ; } private static void saveJsonEnt (String jsonType,String jsonId,String jsonName,String parentId,Long levelId) throws Exception { logger.info(jsonType+"/"+jsonId+"/"+jsonName+"/"+parentId+"/"+String.valueOf(levelId)); CommJsonData ent = new CommJsonData(); ent.setJsonType(jsonType); ent.setJsonCode(jsonId); ent.setJsonName(jsonName); ent.setRowId(StringUtil.getUUID()); ent.setParentCode(parentId); ent.setLevelId(levelId); IDBSupportService service = (IDBSupportService)ServiceLocator.getBean("IDBSupportService3"); service.saveOrUpdate(ent); } private static String dealJson(JsonItem[] jsonItem,String parentId,String jsonType,Long level) { String sReturn = ""; if(jsonItem!=null&&jsonItem.length>0) { for(int i=0;i<jsonItem.length;i++) { JsonItem ent = jsonItem[i]; //System.out.println(ent.getSub_id()); //System.out.println(ent.getSub_name()); try { saveJsonEnt(jsonType,ent.getSub_id(),ent.getSub_name(),parentId,level+1); } catch(Exception ex) { ex.printStackTrace(); } if(ent.getItems()!=null) { //System.out.println("子項數:"+ent.getItems().length); dealJson(ent.getItems(),ent.getSub_id(),jsonType,level+1); } else { //System.out.println("沒有子項!"); } } } //此函數 return sReturn ; }
示例數據(部門):
{ "name": "全國", "id": "0000000000", "description": "崇德易城市數據", "modified": "2012年08月", "copyright": "http://www.chongdeyi.com/", "items": [ { "city": "北京市", "city_id": "1001000000", "items": [ { "sub_city":"東城區", "sub_id":"2001001000" }, { "sub_city":"西城區", "sub_id":"2001002000" }, { "sub_city":"旭日區", "sub_id":"2001006000" }, { "sub_city":"豐台區", "sub_id":"2001007000" }, { "sub_city":"石景山區", "sub_id":"2001008000" }, { "sub_city":"海澱區", "sub_id":"2001009000" }, { "sub_city":"門頭溝區", "sub_id":"2001010000" }, { "sub_city":"房山區", "sub_id":"2001011000" }, { "sub_city":"通州區", "sub_id":"2001012000" }, { "sub_city":"順義區", "sub_id":"2001013000" }, { "sub_city":"昌平區", "sub_id":"2001014000" }, { "sub_city":"年夜興區", "sub_id":"2001015000" }, { "sub_city":"懷柔區", "sub_id":"2001016000" }, { "sub_city":"平谷區", "sub_id":"2001017000" }, { "sub_city":"延慶縣", "sub_id":"2001018000" }, { "sub_city":"密雲縣", "sub_id":"2001019000" }] },{ "city": "天津市", "city_id": "1002000000", "items": [ { "sub_city":"戰爭區", "sub_id":"2002001000" }
以上所述是小編給年夜家引見的JSON龐雜數據處置之Json樹形構造數據轉Java對象並存儲到數據庫的完成