在网站开发中经常遇到级联数据的展示,比如选择城市的时候弹出的省市县选择界面。很多前端制作人员习惯于从JSON中而不是从数据库中获取省市县数据。那么在选择了省市县中的某一个城市 ,存储到数据库中需要存储所选城市的代码。所以需要一个能将JSON数据(一般存储在javascript脚本中)结构全部导入到数据库中的功能。
JSON的特点是支持层级结构、支持数组表示的对象 。下面的示例介绍如何将JSON的省市县数据保存到数据库中,实现原理非常简单,就是利用JSON的java工具包API,将层次结构的JSON对象数组通过递归的方式首选转换为Java对象数组,然后保存到数据库中。
实现步骤是:
(一)首先定义一个JsonItem实体类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
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数据文件,并进行递归调用 :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
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 ; } |
示例数据(部分):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
{ "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对象并存储到数据库的实现