first commit
This commit is contained in:
86
src/main/java/com/peanut/common/utils/ReadProvinceUtil.java
Normal file
86
src/main/java/com/peanut/common/utils/ReadProvinceUtil.java
Normal file
@@ -0,0 +1,86 @@
|
||||
package com.peanut.common.utils;
|
||||
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.peanut.modules.book.entity.CityEntity;
|
||||
import com.peanut.modules.book.entity.CountyEntity;
|
||||
import com.peanut.modules.book.entity.ProvinceEntity;
|
||||
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<ProvinceEntity> getFile(String filePath) {
|
||||
JSONArray proJsonArray = null;
|
||||
JSONArray cityJsonArray = null;
|
||||
JSONArray countyJsonArray = null;
|
||||
|
||||
JSONObject cityJson = null;
|
||||
JSONObject countyJson = null;
|
||||
Date date = new Date();
|
||||
|
||||
List<ProvinceEntity> provinceList = null;
|
||||
List<CityEntity> cityList = null;
|
||||
List<CountyEntity> 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<ProvinceEntity>();
|
||||
if (proJsonArray != null) {
|
||||
for (int i = 0; i < proJsonArray.size(); i++) {
|
||||
ProvinceEntity province = new ProvinceEntity();
|
||||
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<CityEntity>();
|
||||
if (cityJsonArray != null) {
|
||||
for (int j = 0; j < cityJsonArray.size(); j++) {
|
||||
CityEntity city = new CityEntity();
|
||||
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<CountyEntity>();
|
||||
if (countyJsonArray != null) {
|
||||
for (int k = 0; k < countyJsonArray.size(); k++) {
|
||||
CountyEntity county = new CountyEntity();
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user