• java解析json文件(省,市,区)


    [{"code":"11","name":"北京市"},{"code":"12","name":"天津市"},{"code":"13","name":"河北省"},{"code":"14","name":"山西省"},{"code":"15","name":"内蒙古自治区"},
    {"code":"21","name":"辽宁省"},{"code":"22","name":"吉林省"},{"code":"23","name":"黑龙江省"},{"code":"31","name":"上海市"},{"code":"32","name":"江苏省"},
    {"code":"33","name":"浙江省"},{"code":"34","name":"安徽省"},{"code":"35","name":"福建省"},{"code":"36","name":"江西省"},{"code":"37","name":"山东省"},
    {"code":"41","name":"河南省"},{"code":"42","name":"湖北省"},{"code":"43","name":"湖南省"},{"code":"44","name":"广东省"},{"code":"45","name":"广西壮族自治区"},
    {"code":"46","name":"海南省"},{"code":"50","name":"重庆市"},{"code":"51","name":"四川省"},{"code":"52","name":"贵州省"},{"code":"53","name":"云南省"},
    {"code":"54","name":"西藏自治区"},{"code":"61","name":"陕西省"},{"code":"62","name":"甘肃省"},{"code":"63","name":"青海省"},{"code":"64","name":"宁夏回族自治区"},
    {"code":"65","name":"新疆维吾尔自治区"}]

    有相同数据类型的province,city,area,street的json文件,通过解析,实现控制台输出。

    建了一个公共类方法,代码如下:
    package com.sdlanji.budd.common;


    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject;

    import java.io.BufferedReader;
    import java.io.ByteArrayInputStream;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;

    /**
    * 用于json文件数据的获取
    *
    *
    *
    */

    public class jsonExport {

    public static JSONArray getJsonArray(String url) {
    //jsonArray[]格式
    JSONArray jsonArray = new JSONArray();
    StringBuilder result = new StringBuilder();
    try {
    BufferedReader br = new BufferedReader(new InputStreamReader(
    new FileInputStream(url), "UTF-8"));
    String s = null;
    while ((s = br.readLine()) != null) {
    result.append(System.lineSeparator() + s);
    }
    br.close();
    jsonArray = JSONArray.parseArray(result.toString());
    } catch (Exception e) {
    e.printStackTrace();
    }
    return jsonArray;
    }

    public static JSONObject getJsonObjectBy(String url){

    JSONObject jsonObject = new JSONObject();
    StringBuilder result = new StringBuilder();
    try{
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
    new FileInputStream(url),"UTF-8"));
    String s = null;
    while((s = bufferedReader.readLine()) != null){
    result.append(System.lineSeparator() + s);
    }
    bufferedReader.close();
    jsonObject = JSONObject.parseObject(result.toString());
    }catch (Exception e){
    e.printStackTrace();
    }
    return jsonObject;
    }
    }

    controller层代码:
    public static void main(String[] args){

    //创建一个文件对象,指定要读取的文件
    // File file = new File("F:/json文件/provinces");
    //省数据
    JSONArray ja = new JSONArray();
    ja = jsonExport.getJsonArray("F:\json文件\provinces.json");
    for(int i=0;i<ja.size();i++) {
    //jsonObject{}格式
    JSONObject jsonObject = ja.getJSONObject(i);
    String proName = jsonObject.get("name").toString();
    Integer proId = jsonObject.getInteger("code");
    System.out.println(proId + "," + proName);
    }

    //市数据
    JSONArray jsonArray = new JSONArray();
    jsonArray = jsonExport.getJsonArray("F:\json文件\cities.json");
    for (int j=0;j<jsonArray.size();j++) {
    JSONObject jsonObject1 = jsonArray.getJSONObject(j);
    String cityName = jsonObject1.get("name").toString();
    Integer provinceCode = jsonObject1.getInteger("provinceCode");
    Integer cityId = jsonObject1.getInteger("code");
    System.out.println( cityName + "," + cityId);
    }

    //区数据
    JSONArray jsonArray1 = new JSONArray();
    jsonArray1 = jsonExport.getJsonArray("F:\json文件\areas.json");
    for (int y=0;y<jsonArray1.size();y++) {
    JSONObject jsonObject = jsonArray1.getJSONObject(y);
    String areaName = jsonObject.get("name").toString();
    Integer code = jsonObject.getInteger("code");
    System.out.println(areaName + "," + code);
    }

    //街道数据
    JSONArray jsonArrayStreet = new JSONArray();
    jsonArrayStreet = jsonExport.getJsonArray("F:\json文件\streets.json");
    Map<Object,Object> list1 = new HashMap<>();
    List<Object> listStreet = new ArrayList<>();
    for (int x=0;x<jsonArrayStreet.size();x++){
    JSONObject jsonObject1 = jsonArrayStreet.getJSONObject(x);
    String streetName = jsonObject1.get("name").toString();
    Integer codeId = jsonObject1.getInteger("code");
    Integer areaId = jsonObject1.getInteger("areaCode");
    System.out.println(streetName + "," + codeId);
    }
    }
    代码输出结果:
    取部分结果

    省:

    11,北京市        
    12,天津市
    13,河北省
    14,山西省
    15,内蒙古自治区
    21,辽宁省
    22,吉林省

    市:

    秦皇岛市,1303
    邯郸市,1304
    邢台市,1305
    保定市,1306
    张家口市,1307
    承德市,1308
    沧州市,1309
    廊坊市,1310
    衡水市,1311

    区:

    亮甲店镇,130229101
    鸦鸿桥镇,130229102
    窝洛沽镇,130229103
    石臼窝镇,130229104
    虹桥镇,130229105
    散水头镇,130229106

    街道:

    人民路街道办事处,659004003
    兵团一零一团,659004500
    兵团一零二团,659004501
    兵团一零三团,659004502
    博古其镇,659006100
    双丰镇,659006101



    现在想把数据拼接起来,街道总共4万多条,不知道要怎么操作了。




  • 相关阅读:
    程序员必备工具之Cmder
    Markdown简明教程
    几种黑灰名词解释
    windows下 mysql启动错误1067进程意外终止
    Mac OS 安装redis
    java里的这些
    maven 两条命令建立 web项目
    判断StringBuilder是否为空
    map与list的交集、补集操作
    关于创业
  • 原文地址:https://www.cnblogs.com/txppp/p/9970041.html
Copyright © 2020-2023  润新知