JAVA根据省市区县获取配置⽂件经纬度(静态)
突然间有个⼩伙伴咨询我根据省市区县获取配置⽂件经纬度,于是我就按照⾃⼰的思路,写了⼀个demo给到⼩伙伴。同时,很感谢我⼤卢进,毕竟我是在他的基础上,做优化的。
本⼈采⽤的maven库,做jar包的托管。
感恩母亲节祝福语⼀、前提准备
1、⾸先导⼊comons-io的依赖
光学工程<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.4</version>
</dependency>
2、声明⼀个DTO (CityDTO)
public class CityDTO {
/**
* 省份编码
*/
private String pCode;
/**
* 省份名称
*/
private String pName;
/**
* 市级编码
*/
private String cCode;
/**
* 市级名称
*/
private String cName;2012古装电视剧
/**
* 区县编码
*/
private String dCode;
/**
* 区县名称
*/
private String dName;
/**
* 经度
*/
private BigDecimal lon;
/**
* 纬度
*/
private BigDecimal lat;
public CityDTO() {
}
public String getpCode() {
return pCode;
}
public void setpCode(String pCode) { this.pCode = pCode;
}
public String getpName() {
return pName;
}
public void setpName(String pName) { this.pName = pName;
}
public String getcCode() {
return cCode;
}
public void setcCode(String cCode) { this.cCode = cCode;
}
public String getcName() {
return cName;
}
public void setcName(String cName) { this.cName = cName;
}
public String getdCode() {
return dCode;
}
public void setdCode(String dCode) { this.dCode = dCode;
}
public String getdName() {
return dName;
}
public void setdName(String dName) { this.dName = dName;
}
public BigDecimal getLon() {
return lon;
}
public void setLon(BigDecimal lon) { this.lon = lon;
}
public BigDecimal getLat() {
return lat;
}
public void setLat(BigDecimal lat) {
this.lat = lat;
this.lat = lat;
}
@Override
public String toString() {
return "CityDTO{" +
"pCode='" + pCode + '\'' +
", pName='" + pName + '\'' +
", cCode='" + cCode + '\'' +
", cName='" + cName + '\'' +
", dCode='" + dCode + '\'' +
", dName='" + dName + '\'' +
", lon=" + lon +
", lat=" + lat +
'}';
}
}
3、声明⼀个返回体(ResultInfo)
public class ResultInfo<T> {
/**
* 0 success,-1 fail
五年级英语试卷分析*/
private int success;
/**
* 错误编码
*/
private String code;
/**
* 消息提⽰
*/
private String message;
/**
* 数据
*/
private T data;
private double totalWarning;
public ResultInfo() {
}
public ResultInfo(int success, String code, String message) {
super();
this.success = success;
}
public ResultInfo(int success, String code, String message, T data) {
super();
this.success = success;
this.data = data;
}
public ResultInfo(int success, String code, String message, T data, double totalWarning) { super();
this.success = success;
this.data = data;
}
public String getCode() {
return code;
}
public void setCode(String code) {
大一学年总结de = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public int getSuccess() {
return success;
}
public void setSuccess(int success) {
this.success = success;
}
public double getTotalWarning() {
return totalWarning;
}
public void setTotalWarning(double totalWarning) {
}
@Override
public String toString() {
return "ResultInfo [success=" + success + ", code=" + code + ", message=" + message + ", data=" + data + ", totalWarning"+ totalWarning + "]"; }
}
三⼂核⼼代码
ParseCityJsonUtils⼯具类 ——> parseJsonMethod⽅法:
public static ResultInfo<CityDTO> parseJsonMethod(Map<String,Object> inParam, String path){
String str = "";
String pName = "";
String cName = "";
String dName = "";
if(null == ("pName")){
return new ResultInfo<>(1,"1111","缺少省份!");
}
pName = String.("pName"));
if(null == ("pName")){
return new ResultInfo<>(1,"1112","缺少市级!");
}
}
cName = String.("cName"));
if(null == ("pName")){
return new ResultInfo<>(1,"1113","缺少区县!");
}
dName = String.("dName"));
CityDTO cityDTO = new CityDTO();
long start = System.currentTimeMillis();
try {
//1.1、读取city.json⽂件,⽤字符串拼接
File filePath = new File(path);
str = adFileToString(filePath, "UTF-8");
//1.2、参数解析
//JSONObject jso = JSONObject.parseObject(str);
JSONObject jsonObject = JSONObject.parseObject(str);
if (jsonObject == null) {
long errorTime1 = System.currentTimeMillis();
return new ResultInfo<>(1,"3333","参数解析异常,耗时---"+(errorTime1-start)+"---ms"); }
String provinces = "";
String city = "";
String area = "";
provinces = String("p");
JSONObject jsonP = JSONObject.parseObject(provinces);
city = String("c");
JSONObject jsonC = JSONObject.parseObject(city);
area = String("d");
JSONObject jsonD = JSONObject.parseObject(area);
//2.1、遍历所有省份键值对
Set<String> setP = jsonP.keySet();
Iterator<String> it1 = setP.iterator();
while (it1.hasNext()) {
String key = ();
String value = String(key);
//包含判断
if (ains(value)) {
cityDTO.setpCode(key);
cityDTO.setpName(pName);
break;
}
}
//3.1、根据省份键,取出城市中对应的数据
String theCity = pCode());
JSONObject jsonCurC= JSONObject.parseObject(theCity);
//3.2、遍历所有市级的键值对
Set<String> setCurC = jsonCurC.keySet();
咯Iterator<String> it2 = setCurC.iterator();
while (it2.hasNext()) {
String key = ();
String value = String(key);
//包含判断
if (ains(value)) {
cityDTO.setcCode(key);
cityDTO.setcName(cName);
break;
}
}
//4.1、根据市级键取出区中对应的数据
String theD = Code());
JSONObject jsonCurD = JSONObject.parseObject(theD);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论