86 lines
3.8 KiB
Java
86 lines
3.8 KiB
Java
package com.peanut.common.utils;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.peanut.modules.book.entity.City;
|
|
import com.peanut.modules.book.entity.County;
|
|
import com.peanut.modules.book.entity.Province;
|
|
import org.apache.commons.io.FileUtils;
|
|
|
|
import java.io.File;
|
|
import java.util.ArrayList;
|
|
import java.util.Date;
|
|
import java.util.List;
|
|
|
|
public class ReadProvinceUtil {
|
|
public static List<Province> getFile(String filePath) {
|
|
JSONArray proJsonArray = null;
|
|
JSONArray cityJsonArray = null;
|
|
JSONArray countyJsonArray = null;
|
|
|
|
JSONObject cityJson = null;
|
|
JSONObject countyJson = null;
|
|
Date date = new Date();
|
|
|
|
List<Province> provinceList = null;
|
|
List<City> cityList = null;
|
|
List<County> countyList = null;
|
|
|
|
// String path = request.getSession().getServletContext().getRealPath("/");
|
|
// path = path + filePath;
|
|
File file = new File(filePath);
|
|
try {
|
|
String input = FileUtils.readFileToString(file, "UTF-8");// 读取文件
|
|
System.out.println(input);
|
|
//文件转换jsonObject格式
|
|
String s = input.replaceAll("\n", "");
|
|
JSONObject proJson = JSONObject.parseObject(s);
|
|
if (proJson != null) {
|
|
// 取出数据
|
|
proJsonArray = proJson.getJSONArray("china");
|
|
provinceList = new ArrayList<Province>();
|
|
if (proJsonArray != null) {
|
|
for (int i = 0; i < proJsonArray.size(); i++) {
|
|
Province province = new Province();
|
|
JSONObject temp = proJsonArray.getJSONObject(i);
|
|
province.setProvName(temp.getString("name"));
|
|
province.setCreateDate(date);
|
|
province.setRegionCode(temp.getString("code"));
|
|
|
|
cityJsonArray = JSONArray.parseArray(temp.getString("cityList"));
|
|
cityList = new ArrayList<City>();
|
|
if (cityJsonArray != null) {
|
|
for (int j = 0; j < cityJsonArray.size(); j++) {
|
|
City city = new City();
|
|
cityJson = cityJsonArray.getJSONObject(j);
|
|
city.setCityName(cityJson.getString("name"));
|
|
city.setRegionCode(cityJson.getString("code"));
|
|
city.setCreateDate(date);
|
|
countyJsonArray = JSONArray.parseArray(cityJson.getString("areaList"));
|
|
countyList = new ArrayList<County>();
|
|
if (countyJsonArray != null) {
|
|
for (int k = 0; k < countyJsonArray.size(); k++) {
|
|
County county = new County();
|
|
countyJson = countyJsonArray.getJSONObject(k);
|
|
county.setCountyName(countyJson.getString("name"));
|
|
county.setRegionCode(countyJson.getString("code"));
|
|
county.setCreateDate(date);
|
|
countyList.add(county);
|
|
}
|
|
city.setCountyList(countyList);
|
|
cityList.add(city);
|
|
}
|
|
}
|
|
province.setCityList(cityList);
|
|
provinceList.add(province);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
e.getStackTrace();
|
|
}
|
|
return provinceList;
|
|
}
|
|
} |