中国天气网城市代码Json数据
生活随笔
收集整理的這篇文章主要介紹了
中国天气网城市代码Json数据
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
城市代碼列表的接口:
http://mobile.weather.com.cn/js/citylist.xml
打開控制臺里,將citylist.xml請求右鍵"SAVE AS"保存下來,或者直接用下載工具也行
xml文件里幾乎包含了所有國家的城市數據,可以將其他國家的數據過濾掉,只留下中國的數據
于是我用Java處理了一下
String[] province = new String[]{"北京","天津","河北","山西","內蒙古","遼寧","吉林","黑龍江","上海","江蘇","浙江","安徽","福建","江西","山東","河南","湖北","湖南","廣東","廣西","海南","重慶","四川","貴州","云南","西藏","陜西","甘肅","青海","寧夏","新疆"}; List<String> provinceList = Arrays.asList(province); File file = new File("citylist.xml"); SAXReader saxReader = new SAXReader(); Document document = saxReader.read(file); Element rootElement = document.getRootElement(); Element cElement = rootElement.element("c"); List<Map<String, String>> mapList = new ArrayList<>(); for (Iterator it = cElement.elementIterator(); it.hasNext();) {Map<String, String> map = new HashMap<>();Element element = (Element) it.next();String d1 = element.attribute("d1").getValue();String d2 = element.attribute("d2").getValue();String d3 = element.attribute("d3").getValue();String d4 = element.attribute("d4").getValue();if (!provinceList.contains(d4)) {continue;}map.put("cityCode", d1);map.put("cityName", d2);map.put("cityPinyin", d3);map.put("province", d4);mapList.add(map); } Gson gson = new Gson(); String s = gson.toJson(mapList); System.out.println(s); FileUtils.writeStringToFile(new File("citylist.json"), s, "utf-8");生成的Json文件
cn_weather_citylist.json
壓縮后的:
cn_weather_citylist_compressed.json
總結
以上是生活随笔為你收集整理的中国天气网城市代码Json数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 软件验收报告文档模版
- 下一篇: dll注入实验