您好,登錄后才能下訂單哦!
本篇內容介紹了“如何使用聚合數據實現Json解析全國市縣區按漢字首字母排序”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.text.Collator; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.Iterator; import java.util.List; import java.util.Locale; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class Test_1 { private static String result_info = ""; static List<String> list_district = new ArrayList<String>(); public static void main(String[] args) { URL url; try { url = new URL( "http://v.juhe.cn/postcode/pcd?dtype=json&key=9da.......");/需要自己申請 聚合數據的 apikey HttpURLConnection urlcon = (HttpURLConnection) url.openConnection(); urlcon.connect(); // 獲取連接 InputStream is = urlcon.getInputStream(); BufferedReader buffer = new BufferedReader(new InputStreamReader( is, "utf-8")); StringBuffer bs = new StringBuffer(); String linputline = null; while ((linputline = buffer.readLine()) != null) { result_info += linputline + "\n"; } // System.out.println(result_info);///所有信息不用輸出 if (!result_info.equals("")) { JSONObject dataJson = JSONObject.fromObject(result_info); System.out.println(dataJson.getString("reason")); // JSONObject result_province = // dataJson.getJSONObject("result"); for (int j = 0; j < 31; j++) { JSONArray result_province_array = dataJson .getJSONArray("result"); String province = result_province_array.getJSONObject(j) .getString("province"); // System.out.println(j + 1 + ": " + province); list_district.add(province); JSONArray result_province_city_array = result_province_array .getJSONObject(j).getJSONArray("city"); for (int k = 0; k < result_province_city_array.size(); k++) { String city = result_province_city_array.getJSONObject( k).getString("city"); String city_id = result_province_city_array .getJSONObject(k).getString("id"); // // System.out.println(j + 1 + city_id + ": " + province // + ":" + city); list_district.add(province + " " + city); JSONArray result_province_city_district_array = result_province_city_array .getJSONObject(k).getJSONArray("district"); for (int m = 0; m < result_province_city_district_array .size(); m++) { String district_id = result_province_city_district_array .getJSONObject(m).getString("id"); String district = result_province_city_district_array .getJSONObject(m).getString("district"); // System.out.println(j + 1 + city_id + district_id // + ": " + province + " " + city + " " // + district); list_district.add(province + " " + city + " " + district); }// /for m }// /for k }// /for j }//if String[] str_district = new String[list_district.size()]; for(int n=0;n<list_district.size();n++){ str_district[n]=list_district.get(n); //System.out.println(str_district[n]); } Comparator c= Collator.getInstance(Locale.CHINA); Arrays.sort(str_district, c); for(int p=0;p<str_district.length;p++){ System.out.println(str_district[p]); } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }///main }
“如何使用聚合數據實現Json解析全國市縣區按漢字首字母排序”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。