读取省市区json文件,插入数据库表,表中保存父子节点对应关系
Posted tower888
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了读取省市区json文件,插入数据库表,表中保存父子节点对应关系相关的知识,希望对你有一定的参考价值。
java读取省市区json文件, 入库表t_city(code,val,fathercode), 保存父子节点
package manager;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
public class CityNodeSqlGen
public static void main(String[] args)
String f = "I:/myData/qq_data/3442175060/FileRecv/cities1.js";
// String f = "I:/myData/qq_data/3442175060/FileRecv/bj.js";
File file = new File(f);
JSONArray jsonArray = JSONUtil.readJSONArray(file, CharsetUtil.charset("UTF-8"));
// System.out.println(json.toString());
List<CityNode> list = JSONUtil.toList(jsonArray, CityNode.class);
// System.out.println(JSONUtil.toJsonStr(cn));
pro(list);
private static void pro(List<CityNode> list)
String sql = null;
List<String> result = new ArrayList<>();
for (CityNode t_city : list)
String code = t_city.getCode();
String val = t_city.getValue();
sql = "insert into t_city(code,val,fathercode) values('" + code + "','" + val + "','" + 0 + "');";//省
// System.out.println(sql);
result.add(sql);
List<CityNode> children = t_city.getChildren();
for (CityNode city : children)
String code1 = city.getCode();
String val1 = city.getValue();
sql = "insert into t_city(code,val,fathercode) values('" + code1 + "','" + val1 + "','" + code
+ "');";//市
// System.out.println(sql);
result.add(sql);
List<CityNode> children2 = city.getChildren();
if (CollUtil.isNotEmpty(children2))
for (CityNode area : children2)
String code2 = area.getCode();
String val2 = area.getValue();
sql = "insert into t_city(code,val,fathercode) values('" + code2 + "','" + val2 + "','"
+ code1 + "');";//区
// System.out.println(sql);
result.add(sql);
File file = new File("I:\\\\项目代码和文档\\\\加油站项目\\\\t_city2.sql");
FileUtil.writeLines(result, file, "utf-8");
package manager;
import java.util.List;
import lombok.Data;
@Data
public class CityNode
private String code;
private String value;
private String label;
private List<CityNode> children;
以上是关于读取省市区json文件,插入数据库表,表中保存父子节点对应关系的主要内容,如果未能解决你的问题,请参考以下文章
如何从单个文件中读取不同的模式 json 对象并将其保存到表中