JSON complex data processing Json tree structured data transfer to Java objects and stored in a database

  • 2020-05-05 11:20:04
  • OfStack

  often encounters the display of cascading data in website development, such as the selection screen of provinces, cities and counties that pops up when selecting a city. Many front-end developers are used to getting city, city and county data from JSON rather than from a database. So in the selection of a city in the provinces and counties, storage to the database needs to store the code of the selected city. So you need a function that imports all the JSON data structures (typically stored in javascript scripts) into the database.

JSON is characterized by support for hierarchical structures and objects represented by arrays. The following example shows how to save JSON's provincial and county data in a database. The implementation principle is very simple, namely, using JSON's java toolkit API, the hierarchical JSON object array is converted into Java object array recursively and then saved into the database.

The implementation steps are:

(I) first define an JsonItem entity class:


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

(ii) define a utility class that reads the Json data file in the utility class and makes a recursive call to


public static String importJson(String fullFileName,String jsonType,String encoding,HttpServletRequest request) throws Exception
{
//Json Convert to entity class reference :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 =" read Json The file failed !";
}
// To obtain rootId
//logger.info("");
jsonData = jsonData.replace("\"items\":\"\"", ""); // Remove empty  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("]"));
// different json the id,name Means not the same, unified by sub_id,subname In order to with JsonItem Class attributes match 
//  After the replacement convenient unified processing 
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");
// will rootData convert array 
rootData = "[" + rootData + "]";
try
{
FileUtil.str2file(rootData, "d:/jsonData.txt", "utf-8");// Store to disk to verify that the string conversion is correct 
}
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(" Item number :"+ent.getItems().length);
dealJson(ent.getItems(),ent.getSub_id(),jsonType,level+1);
}
else
{
//System.out.println(" There is no child !");
}
}
}
// This function  
return sReturn ;
} 

Sample data (part) :


{
"name": " The national ",
"id": "0000000000",
"description": " Chongdeyi city data ",
"modified": "2012 years 08 month ",
"copyright": "http://www.chongdeyi.com/",
"items": [
{
"city": " The Beijing municipal ",
"city_id": "1001000000",
"items": [
{
"sub_city":" The dongcheng district ",
"sub_id":"2001001000"
},
{
"sub_city":" Xicheng district ",
"sub_id":"2001002000"
},
{
"sub_city":" Chaoyang district ",
"sub_id":"2001006000"
},
{
"sub_city":" Fengtai district ",
"sub_id":"2001007000"
},
{
"sub_city":" The shijingshan district ",
"sub_id":"2001008000"
},
{
"sub_city":" Haidian district, ",
"sub_id":"2001009000"
},
{
"sub_city":" Mentougou district ",
"sub_id":"2001010000"
},
{
"sub_city":" Fangshan district ",
"sub_id":"2001011000"
},
{
"sub_city":" Tongzhou district ",
"sub_id":"2001012000"
},
{
"sub_city":" Shunyi district ",
"sub_id":"2001013000"
},
{
"sub_city":" Changping district ",
"sub_id":"2001014000"
},
{
"sub_city":" Daxing district ",
"sub_id":"2001015000"
},
{
"sub_city":" Huairou district ",
"sub_id":"2001016000"
},
{
"sub_city":" Pinggu district ",
"sub_id":"2001017000"
},
{
"sub_city":" Yanqing county ",
"sub_id":"2001018000"
},
{
"sub_city":" Miyun county ",
"sub_id":"2001019000"
}]
},{
"city": " tianjin ",
"city_id": "1002000000",
"items": [
{
"sub_city":" Heping district ",
"sub_id":"2002001000"
}

The above is an

implementation of JSON complex data processing, Json tree-structured data transfer to Java objects and stored in a database

Related articles: