本篇内容介绍了“如何使用聚合数据实现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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。