Java代碼: dept.json就是高德接口傳回的完整json,我儲存到了本地處理的。
public class LocationTest {
public static void main(String[] args) throws IOException {
initDb();
//高德的接口描述頁面
//http://lbs.amap.com/api/webservice/guide/api/district/#district
//接口位址
//http://restapi.amap.com/v3/config/district?key=&keywords=中國&subdistrict=3&showbiz=false&extensions=base
File file = new File("C:\\Users\\tj\\Desktop\\dept.json");
String s = FileUtils.readFileToString(file, "utf-8");
JSONObject o = JSON.parseObject(s).getJSONArray("districts").getJSONObject(0);
save(o, 0, ",0,",1);
}
static int id = 10000;
public static int nextId(){
id++;
return id;
}
public static void save(JSONObject o,int pid,String ppath,int depth){
String adcode = o.getString("adcode");
String name = o.getString("name");
String level = o.getString("level");
String center = o.getString("center");
String lat = center.split(",")[1];
String lont = center.split(",")[0];
Area area = new Area();
area.set("adcode",adcode);
String citycode = o.getString("citycode");
if(!citycode.equals("[]")){
area.set("citycode",citycode);
}
int id = nextId();
area.set("id", id);
area.set("name",name);
area.set("level",level);
area.set("lat",lat);
area.set("lont",lont);
area.set("adcode",adcode);
area.set("spell", PinyinHelper.convertToPinyinString(name,"",PinyinFormat.WITHOUT_TONE));
area.set("short_spell", PinyinHelper.getShortPinyin(name));
area.set("pid",pid);
String mypath = ppath+id+",";
area.set("path",mypath);
area.set("depth", depth);
boolean b = true;
try {
b = area.save();
} catch (Exception e) {
e.printStackTrace();
}
if(!b) {
System.out.println("儲存失敗:" + name+" adcode:"+id);
}
JSONArray array = o.getJSONArray("districts");
for (int i = 0; i < array.size(); i++) {
JSONObject so = array.getJSONObject(i);
save(so, id, mypath,depth+1);
}
}
public static void initDb(){
String jdbcUrl = "jdbc:mysql://localhost:3306/wd_qx?characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull";
DruidPlugin druidPlugin = new DruidPlugin(jdbcUrl, "root", "password","com.mysql.jdbc.Driver");
druidPlugin.addFilter(new WallFilter());
druidPlugin.addFilter(new StatFilter());
druidPlugin.start();
ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
arp.addMapping("ts_area_tj", "id", Area.class);
arp.setDialect(new MysqlDialect());
arp.start();
}
}
表結構:
CREATE TABLE `ts_area` (
`id` varchar(15) NOT NULL,
`name` varchar(60) DEFAULT NULL,
`adcode` varchar(20) DEFAULT NULL,
`citycode` varchar(20) DEFAULT NULL,
`pid` varchar(20) DEFAULT NULL,
`level` varchar(20) DEFAULT NULL,
`lat` double DEFAULT NULL,
`lont` double DEFAULT NULL,
`spell` varchar(100) DEFAULT NULL,
`short_spell` varchar(200) DEFAULT NULL,
`path` varchar(1000) DEFAULT NULL,
`depth` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
依賴的jar:

上傳不了附件,sql沒辦法傳上來了。