• 在json格式的数据里筛选出指定的key值


    • 在全国城市数据json格式的文本里筛选出几个指定的key

    • 部分json格式数据如下

        {"sysCatPk": 459,
        "prntSysCatPk": null,
        "prntCd": "9100",
        "prntNmEn": "china",
        "prntNmCn": "中国",
        "cd": "9100.100",
        "nmEn": "beijingshi",
        "nmCn": "北京市",
        "methSysCatPk": 252,
        "methCd": "9100",
        "methNmEn": "china",
        "methNmCn": "中国",
        "desc": "",
        "viewSeq": "",
        "busSeq": "",
        "value": null,
        "level": 1,
        "childSysCatDtozList": [{
        	"sysCatPk": 460,
        	"prntSysCatPk": 459,
        	"prntCd": "9100.100",
        	"prntNmEn": "beijingshi",
        	"prntNmCn": "北京市",
        	"cd": "9100.100.100",
        	"nmEn": "beijingshi",
        	"nmCn": "北京市",
        	"methSysCatPk": 252,
        	"methCd": "9100",
        	"methNmEn": "china",
        	"methNmCn": "中国",
        	"desc": "",
        	"viewSeq": "",
        	"busSeq": "",
        	"value": null,
        	"level": 2,
        	"childSysCatDtozList": [{
        		"sysCatPk": 461,
        		"prntSysCatPk": 460,
        		"prntCd": "9100.100.100",
        		"prntNmEn": "beijingshi",
        		"prntNmCn": "北京市",
        		"cd": "9100.100.100.100",
        		"nmEn": "dongchengqu",
        		"nmCn": "东城区",
        		"methSysCatPk": 252,
        		"methCd": "9100",
        		"methNmEn": "china",
        		"methNmCn": "中国",
        		"desc": "",
        		"viewSeq": "",
        		"busSeq": "",
        		"value": null,
        		"level": 3,
        		"childSysCatDtozList": [],
        		"top": false,
        		"leaf": true
        	},
        	{
        		"sysCatPk": 462,
        		"prntSysCatPk": 460,
        		"prntCd": "9100.100.100",
        		"prntNmEn": "beijingshi",
        		"prntNmCn": "北京市",
        		"cd": "9100.100.100.110",
        		"nmEn": "xichengqu",
        		"nmCn": "西城区",
        		"methSysCatPk": 252,
        		"methCd": "9100",
        		"methNmEn": "china",
        		"methNmCn": "中国",
        		"desc": "",
        		"viewSeq": "",
        		"busSeq": "",
        		"value": null,
        		"level": 3,
        		"childSysCatDtozList": [],
        		"top": false,
        		"leaf": true
        	},
        	{
        		"sysCatPk": 463,
        		"prntSysCatPk": 460,
        		"prntCd": "9100.100.100",
        		"prntNmEn": "beijingshi",
        		"prntNmCn": "北京市",
        		"cd": "9100.100.100.120",
        		"nmEn": "chongwenqu",
        		"nmCn": "崇文区",
        		"methSysCatPk": 252,
        		"methCd": "9100",
        		"methNmEn": "china",
        		"methNmCn": "中国",
        		"desc": "",
        		"viewSeq": "",
        		"busSeq": "",
        		"value": null,
        		"level": 3,
        		"childSysCatDtozList": [],
        		"top": false,
        		"leaf": true
        	}],
        	"top": false,
        	"leaf": false
        }],
        "top": true,
        "leaf": false
      }
      
    • 需要的json格式如下,只选取三个字段

        [{"sysCatPk":"459","nmCn":"北京市","childSysCatDtozList":
        		[{"sysCatPk":"460","nmCn":"北京市","childSysCatDtozList":
        			[{"sysCatPk":"461","childSysCatDtozList":[],"nmCn":"东城区"},
        			{"sysCatPk":"462","childSysCatDtozList":[],"nmCn":"西城区"},
        			{"sysCatPk":"463","childSysCatDtozList":[],"nmCn":"崇文区"},
        			{"sysCatPk":"464","childSysCatDtozList":[],"nmCn":"宣武区"},
        			{"sysCatPk":"465","childSysCatDtozList":[],"nmCn":"朝阳区"},
        			{"sysCatPk":"466","childSysCatDtozList":[],"nmCn":"丰台区"},
        			{"sysCatPk":"467","childSysCatDtozList":[],"nmCn":"石景山区"},
        			{"sysCatPk":"468","childSysCatDtozList":[],"nmCn":"海淀区"},
        			{"sysCatPk":"469","childSysCatDtozList":[],"nmCn":"门头沟区"},
        			{"sysCatPk":"470","childSysCatDtozList":[],"nmCn":"房山区"},
        			{"sysCatPk":"471","childSysCatDtozList":[],"nmCn":"通州区"},
        			{"sysCatPk":"472","childSysCatDtozList":[],"nmCn":"顺义区"},
        			{"sysCatPk":"473","childSysCatDtozList":[],"nmCn":"昌平区"},
        			{"sysCatPk":"474","childSysCatDtozList":[],"nmCn":"大兴区"},
        			{"sysCatPk":"475","childSysCatDtozList":[],"nmCn":"平谷区"},
        			{"sysCatPk":"476","childSysCatDtozList":[],"nmCn":"怀柔区"},
        			{"sysCatPk":"477","childSysCatDtozList":[],"nmCn":"密云县"},
        			{"sysCatPk":"478","childSysCatDtozList":[],"nmCn":"延庆县"}
        		}]
        }]
      
    • 实现方法:

      • 创建一个实体类
      @Data
      public class City {
      		private String sysCatPk;
      		private String nmCn;
       	private List<City> childSysCatDtozList;
      }
      
      • 从文本中读取json数据
      public class TextRead {
        public static String readFileContent(String fileName) {
           File file = new File(fileName);
           BufferedReader reader = null;
           StringBuffer sbf = new StringBuffer();
           try {
              reader = new BufferedReader(new FileReader(file));
              String tempStr;
              while ((tempStr = reader.readLine()) != null) {
                  sbf.append(tempStr);
              }
              reader.close();
              return sbf.toString();
            } catch (IOException e) {
              e.printStackTrace();
            } finally {
              if (reader != null) {
                  try {
                      reader.close();
                  } catch (IOException e1) {
                      e1.printStackTrace();
                  }
              }
          }
          	return sbf.toString();
      	}
      }
      
      • 转换
       public static void main(String[] asg) throws IOException {
             String str = TextRead.readFileContent("C:\Users\mengzhao\Desktop\全国城市数据json格式.txt");
             JSONArray array = JSONArray.parseArray(str);
             List<City> cities = array.toJavaList(City.class);
             Object json = JSONArray.toJSON(cities);
             File file = new File("C:\Users\mengzhao\Desktop\全国城市数据json格式1.txt");
             Writer out = new FileWriter(file);
             out.write(json.toString());
             out.close();
      }
      
  • 相关阅读:
    uC/OS-II内核的服务文件
    uC/OS-II汇编代码
    uC/OS-II类型定义
    uC/OS-II核心(Os_core)块
    uC/OS-II配置文件
    uC/OS-II应用程序exe
    uC/OS-II应用程序代码
    技术人员如何创业《四》- 打造超强执行力团队(转载)
    最近做抽奖的活动
    install docker
  • 原文地址:https://www.cnblogs.com/mengzhao/p/13852743.html
Copyright © 2020-2023  润新知