生活随笔
收集整理的這篇文章主要介紹了
java代码把行政区划代码转json格式及sql
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
數據來源:中華人民共和國民政部
直接上代碼:
import com
.alibaba
.fastjson
.JSON
;import java
.io
.*
;
import java
.util
.ArrayList
;
import java
.util
.List
;public class ZoneCodeUtils {public static void main(String
[] args
) throws IOException
{String inPathName
= "C:\\Users\\WBT\\Desktop\\in.txt";String jsonPathName
= "C:\\Users\\WBT\\Desktop\\out.json";String sqlPathName
= "C:\\Users\\WBT\\Desktop\\out.sql";convertToJsonFile(inPathName
, jsonPathName
);convertToSqlFile(inPathName
, sqlPathName
);}private static void convertToJsonFile(String inPathName
, String outPathName
) throws IOException
{File inFile
= new File(inPathName
);BufferedReader reader
= new BufferedReader(new InputStreamReader(new FileInputStream(inFile
), "GBK"));String line
= null
;List
<Zone> provinceList
= new ArrayList<>();Zone provinceZone
= new Zone();List
<Zone> cityList
= new ArrayList<>();Zone cityZone
= new Zone();List
<Zone> countyList
= new ArrayList<>();Zone countyZone
= new Zone();while ((line
= reader
.readLine()) != null
) {String newLine
= line
.trim();if (newLine
.length() < 6) {continue;}String code
= newLine
.substring(0, 6);try {Integer
.parseInt(code
);} catch (NumberFormatException e
) {continue;}String name
= newLine
.substring(7).trim();if (code
.endsWith("0000")) {provinceZone
= new Zone(code
, name
);provinceList
.add(provinceZone
);cityList
= new ArrayList<>();continue;}if (code
.endsWith("00") || code
.startsWith("11") || code
.startsWith("12")|| code
.startsWith("31") || code
.startsWith("50")) {cityZone
= new Zone(code
, name
);cityList
.add(cityZone
);provinceZone
.setChildren(cityList
);countyList
= new ArrayList<>();continue;}countyZone
= new Zone(code
, name
);countyList
.add(countyZone
);cityZone
.setChildren(countyList
);}reader
.close();String result
= JSON
.toJSON(provinceList
).toString();convertTextToLocalFile(outPathName
, result
);}private static void convertToJsonFile2(String inPathName
, String outPathName
) throws IOException
{File inFile
= new File(inPathName
);BufferedReader reader
= new BufferedReader(new InputStreamReader(new FileInputStream(inFile
), "GBK"));String line
= null
;List
<Zone> provinceList
= new ArrayList<>();Zone provinceZone
= new Zone();List
<Zone> cityList
= new ArrayList<>();Zone cityZone
= new Zone();List
<Zone> countyList
= new ArrayList<>();Zone countyZone
= new Zone();String lastCode
= "";while ((line
= reader
.readLine()) != null
) {String newLine
= line
.trim();if (newLine
.length() < 6) {continue;}String code
= newLine
.substring(0, 6);try {Integer
.parseInt(code
);} catch (NumberFormatException e
) {continue;}String name
= newLine
.substring(7).trim();if ((code
.endsWith("0000") || code
.endsWith("00")) && lastCode
.endsWith("00") && !lastCode
.endsWith("0000")) {List
<Zone> tempCityZoneList
= provinceList
.get(provinceList
.size() - 1).getChildren();Zone tempCityZone
= tempCityZoneList
.get(tempCityZoneList
.size() - 1);List
<Zone> tempCountyZoneList
= new ArrayList<>();tempCountyZoneList
.add(new Zone(tempCityZone
.getCode(), tempCityZone
.getName()));tempCityZone
.setChildren(tempCountyZoneList
);}lastCode
= code
;if ("710000".equals(code
) || "810000".equals(code
) || "820000".equals(code
)) {Zone tempCountyZone
= new Zone(code
, name
);List
<Zone> tempCountyZoneList
= new ArrayList<>();tempCountyZoneList
.add(tempCountyZone
);Zone tempCityZone
= new Zone(code
, name
, tempCountyZoneList
);List
<Zone> tempCityZoneList
= new ArrayList<>();tempCityZoneList
.add(tempCityZone
);provinceZone
= new Zone(code
, name
, tempCityZoneList
);provinceList
.add(provinceZone
);cityList
= new ArrayList<>();continue;}if (code
.endsWith("0000")) {provinceZone
= new Zone(code
, name
);provinceList
.add(provinceZone
);cityList
= new ArrayList<>();continue;}if (code
.endsWith("00")) {cityZone
= new Zone(code
, name
);cityList
.add(cityZone
);provinceZone
.setChildren(cityList
);countyList
= new ArrayList<>();continue;}if (code
.startsWith("11") || code
.startsWith("12")|| code
.startsWith("31") || code
.startsWith("50")) {List
<Zone> tempList
= new ArrayList<>();tempList
.add(new Zone(code
, name
));cityZone
= new Zone(code
, name
, tempList
);cityList
.add(cityZone
);provinceZone
.setChildren(cityList
);countyList
= new ArrayList<>();continue;}countyZone
= new Zone(code
, name
);countyList
.add(countyZone
);cityZone
.setChildren(countyList
);}reader
.close();String result
= JSON
.toJSON(provinceList
).toString();convertTextToLocalFile(outPathName
, result
);}private static void convertToSqlFile(String inPathName
, String outPathName
) throws IOException
{File file
= new File(inPathName
);BufferedReader reader
= new BufferedReader(new InputStreamReader(new FileInputStream(file
), "GBK"));String line
= null
;StringBuilder builder
= new StringBuilder();while ((line
= reader
.readLine()) != null
) {String newLine
= line
.trim();if (newLine
.length() < 6) {continue;}String code
= newLine
.substring(0, 6);try {Integer
.parseInt(code
);} catch (NumberFormatException e
) {continue;}String name
= newLine
.substring(7).trim();if (code
.endsWith("0000")) {builder
.append("insert into zone_code (parent, code, name) value ('0', '").append(code
).append("', '").append(name
).append("');").append("\r\n");continue;}if (code
.endsWith("00") || code
.startsWith("11") || code
.startsWith("12")|| code
.startsWith("31") || code
.startsWith("50")) {String parentCode
= code
.substring(0, 2) + "0000";builder
.append("insert into zone_code (parent, code, name) value ('").append(parentCode
).append("', '").append(code
).append("', '").append(name
).append("');").append("\r\n");continue;}String parentCode
= code
.substring(0, 4) + "00";builder
.append("insert into zone_code (parent, code, name) value ('").append(parentCode
).append("', '").append(code
).append("', '").append(name
).append("');").append("\r\n");}reader
.close();convertTextToLocalFile(outPathName
, builder
.toString());}private static void convertTextToLocalFile(String fileName
, String text
) throws IOException
{File file
= new File(fileName
);if (!file
.exists()) {file
.createNewFile();}BufferedOutputStream outputStream
= new BufferedOutputStream(new FileOutputStream(file
));outputStream
.write(text
.getBytes());outputStream
.flush();outputStream
.close();}static class Zone {private String code
;private String name
;private List
<Zone> children
;public String
getCode() {return code
;}public void setCode(String code
) {this.code
= code
;}public String
getName() {return name
;}public void setName(String name
) {this.name
= name
;}public List
<Zone> getChildren() {return children
;}public void setChildren(List
<Zone> children
) {this.children
= children
;}public Zone() {super();}public Zone(String code
, String name
) {this.code
= code
;this.name
= name
;}public Zone(String code
, String name
, List
<Zone> children
) {this.code
= code
;this.name
= name
;this.children
= children
;}}
}
總結一下,寫的不是很好,但是完全夠用,畢竟只是用一次,又不是放在系統中天天用。
之所以自己寫是因為網上找的基本不符合我的需求,或者代碼引用了太多的第三方依賴,很頭疼。我只是引了一個alibaba的json轉換工具包,別的都是java的包。當然不一定用fastjson工具包,只要能把對象轉json就行,然后自己稍微改下報錯的代碼。
sql的話,只是舉個例子,根據自己數據庫表的實際情況進行修改。
原數據只支持官方的數據源,如下圖格式。
總結
以上是生活随笔為你收集整理的java代码把行政区划代码转json格式及sql的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。