获取省市区

github下载地址(获取国家统计局的数据并转成json文件)

https://github.com/small-dream/China_Province_City

解析json文件

String file = "C:\\Users\\2020年10月中华人民共和国县以上行政区划代码.json";
        InputStream inputStream = new FileInputStream(new File(file));
        byte[] bytes = new byte[inputStream.available()];
        StringBuffer buffer = new StringBuffer();
        //读取文件
        inputStream.read(bytes);
        buffer.append(new String(bytes, Charsets.UTF_8));
        inputStream.close();
        //解析JSON文件
        JSONArray all = JSONArray.parseArray(buffer.toString());
        List<Province> provinces = new ArrayList<>(all.size());
        for (int i = 0; i < all.size(); i++) {
            JSONObject _province = all.getJSONObject(i);
            //省
            Province province = new Province();
            province.setName(_province.getString("name"));
            province.setCode(_province.getString("code"));
            //市
            JSONArray _cityList = _province.getJSONArray("cityList");
            List<City> cityList = new ArrayList<>(_cityList.size());
            for (int j = 0; j < _cityList.size(); j++) {
                JSONObject _city = _cityList.getJSONObject(j);
                City city = new City();
                city.setCode(_city.getString("code"));
                city.setName(_city.getString("name"));
                city.setParentCode(_province.getString("code"));
                //区
                JSONArray _areaList = _city.getJSONArray("areaList");
                List<Area> areaList = new ArrayList<>(_areaList.size());
                for (int k = 0; k < _areaList.size(); k++) {
                    JSONObject _area = _areaList.getJSONObject(k);
                    Area area = new Area();
                    area.setCode(_area.getString("code"));
                    area.setName(_area.getString("name"));
                    area.setParentCode(_city.getString("code"));
                    areaList.add(area);
                }
                city.setAreaList(areaList);
                cityList.add(city);
            }
            province.setCityList(cityList);
            //到这里数据解析完成,全部存入provinces
            provinces.add(province);
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值