|
@@ -0,0 +1,51 @@
|
|
|
+package com.bosshand.virgo.api.util;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
+import org.springframework.util.ResourceUtils;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 获取城市code
|
|
|
+ */
|
|
|
+public class CityUtil {
|
|
|
+
|
|
|
+ public static String getCode(String name) {
|
|
|
+ try {
|
|
|
+ File file = ResourceUtils.getFile("classpath:city.json");
|
|
|
+ String json = FileUtils.readFileToString(file, "UTF-8");
|
|
|
+ JSONArray jsonArray = JSONObject.parseArray(json);
|
|
|
+ Map<String, String> code = new HashMap<>();
|
|
|
+ if (code.size() > 0) {
|
|
|
+ return code.get(name);
|
|
|
+ }
|
|
|
+ analysisJson(code, jsonArray);
|
|
|
+ return code.get(name);
|
|
|
+ } catch (IOException o) {
|
|
|
+ o.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void analysisJson(Map<String, String> code, JSONArray jsonArray) {
|
|
|
+ for (int i = 0; i < jsonArray.size(); i++) {
|
|
|
+ JSONArray cityList = jsonArray.getJSONObject(i).getJSONArray("cityList");
|
|
|
+ for (int j = 0; j < cityList.size(); j++) {
|
|
|
+ JSONArray disList = cityList.getJSONObject(j).getJSONArray("disList");
|
|
|
+ for (int h = 0; h < disList.size(); h++) {
|
|
|
+ JSONObject jsonObject = disList.getJSONObject(h);
|
|
|
+ code.put(jsonObject.getString("district"), jsonObject.getString("stationid"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ System.out.println(getCode("西城"));
|
|
|
+ }
|
|
|
+}
|