java解析json檔案(省,市,區)
阿新 • • 發佈:2018-11-16
[{"code":"11","name":"北京市"},{"code":"12","name":"天津市"},{"code":"13","name":"河北省"},{"code":"14","name":"山西省"},{"code":"15","name":"內蒙古自治區"},
{"code":"21","name":"遼寧省"},{"code":"22","name":"吉林省"},{"code":"23","name":"黑龍江省"},{"code":"31","name":"上海市"},{"code":"32","name":"江蘇省"},
{"code":"33","name":"浙江省"},{"code":"34","name":"安徽省"},{"code":"35","name":"福建省"},{"code":"36","name":"江西省"},{"code":"37","name":"山東省"},
{"code":"41","name":"河南省"},{"code":"42","name":"湖北省"},{"code":"43","name":"湖南省"},{"code":"44","name":"廣東省"},{"code":"45","name":"廣西壯族自治區"},
{"code":"46","name":"海南省"},{"code":"50","name":"重慶市"},{"code":"51","name":"四川省"},{"code":"52","name":"貴州省"},{"code":"53","name":"雲南省"},
{"code":"54","name":"西藏自治區"},{"code":"61","name":"陝西省"},{"code":"62","name":"甘肅省"},{"code":"63","name":"青海省"},{"code":"64","name":"寧夏回族自治區"},
{"code":"65","name":"新疆維吾爾自治區"}]
有相同資料型別的province,city,area,street的json檔案,通過解析,實現控制檯輸出。
建了一個公共類方法,程式碼如下:
package com.sdlanji.budd.common;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
/**
* 用於json檔案資料的獲取
*
*
*
*/
public class jsonExport {
public static JSONArray getJsonArray(String url) {
//jsonArray[]格式
JSONArray jsonArray = new JSONArray();
StringBuilder result = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(url), "UTF-8"));
String s = null;
while ((s = br.readLine()) != null) {
result.append(System.lineSeparator() + s);
}
br.close();
jsonArray = JSONArray.parseArray(result.toString());
} catch (Exception e) {
e.printStackTrace();
}
return jsonArray;
}
public static JSONObject getJsonObjectBy(String url){
JSONObject jsonObject = new JSONObject();
StringBuilder result = new StringBuilder();
try{
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
new FileInputStream(url),"UTF-8"));
String s = null;
while((s = bufferedReader.readLine()) != null){
result.append(System.lineSeparator() + s);
}
bufferedReader.close();
jsonObject = JSONObject.parseObject(result.toString());
}catch (Exception e){
e.printStackTrace();
}
return jsonObject;
}
}
controller層程式碼:
public static void main(String[] args){
//建立一個檔案物件,指定要讀取的檔案
// File file = new File("F:/json檔案/provinces");
//省資料
JSONArray ja = new JSONArray();
ja = jsonExport.getJsonArray("F:\\json檔案\\provinces.json");
for(int i=0;i<ja.size();i++) {
//jsonObject{}格式
JSONObject jsonObject = ja.getJSONObject(i);
String proName = jsonObject.get("name").toString();
Integer proId = jsonObject.getInteger("code");
System.out.println(proId + "," + proName);
}
//市資料
JSONArray jsonArray = new JSONArray();
jsonArray = jsonExport.getJsonArray("F:\\json檔案\\cities.json");
for (int j=0;j<jsonArray.size();j++) {
JSONObject jsonObject1 = jsonArray.getJSONObject(j);
String cityName = jsonObject1.get("name").toString();
Integer provinceCode = jsonObject1.getInteger("provinceCode");
Integer cityId = jsonObject1.getInteger("code");
System.out.println( cityName + "," + cityId);
}
//區資料
JSONArray jsonArray1 = new JSONArray();
jsonArray1 = jsonExport.getJsonArray("F:\\json檔案\\areas.json");
for (int y=0;y<jsonArray1.size();y++) {
JSONObject jsonObject = jsonArray1.getJSONObject(y);
String areaName = jsonObject.get("name").toString();
Integer code = jsonObject.getInteger("code");
System.out.println(areaName + "," + code);
}
//街道資料
JSONArray jsonArrayStreet = new JSONArray();
jsonArrayStreet = jsonExport.getJsonArray("F:\\json檔案\\streets.json");
Map<Object,Object> list1 = new HashMap<>();
List<Object> listStreet = new ArrayList<>();
for (int x=0;x<jsonArrayStreet.size();x++){
JSONObject jsonObject1 = jsonArrayStreet.getJSONObject(x);
String streetName = jsonObject1.get("name").toString();
Integer codeId = jsonObject1.getInteger("code");
Integer areaId = jsonObject1.getInteger("areaCode");
System.out.println(streetName + "," + codeId);
}
}
程式碼輸出結果:
取部分結果
省:
11,北京市
12,天津市
13,河北省
14,山西省
15,內蒙古自治區
21,遼寧省
22,吉林省
市:
秦皇島市,1303
邯鄲市,1304
邢臺市,1305
保定市,1306
張家口市,1307
承德市,1308
滄州市,1309
廊坊市,1310
衡水市,1311
區:
亮甲店鎮,130229101
鴉鴻橋鎮,130229102
窩洛沽鎮,130229103
石臼窩鎮,130229104
虹橋鎮,130229105
散水頭鎮,130229106
街道:
人民路街道辦事處,659004003
兵團一零一團,659004500
兵團一零二團,659004501
兵團一零三團,659004502
博古其鎮,659006100
雙豐鎮,659006101
現在想把資料拼接起來,街道總共4萬多條,不知道要怎麼操作了。