【json的处理】四、json-flattener的处理
目前处理json的方法有很多,这里主要总结四种方法
1. Gson方式处理json 【json的处理】一、Gson处理
2. FastJson方式处理json 【json的处理】三、FastJson的处理
3. Jackson方式处理json 【json的处理】二、Jackson的处理
4. json-flattener方式处理json 【json的处理】四、json-flattener的处理
本文主要介绍json-flattener的方式
github地址:https://github.com/wnameless/json-flattener
前言
下面主要通过几个方面来介绍json-flattener:
1. 扁平化处理
2. 去扁平化
引入依赖
<!--依赖 --> <dependency> <groupId>com.github.wnameless.json</groupId> <artifactId>json-flattener</artifactId> <version>0.8.1</version> </dependency>
一、扁平化处理
//json { "ip":"192.168.100.100", "type":"0", "index":"3", "timestamp":"1555317936", "value": [ { "cpuIndex":"0", "cpuName":"Intel(R) Xeon(R) CPUE5-2620 v4", "cpuCores":"8", "cpuFreq":"2.10GHz", "cpuRate":"0.85" }, { "cpuIndex":"1", "cpuName":"Intel(R) Xeon(R) CPUE5-2620 v4", "cpuCores":"8", "cpuFreq":"2.10GHz", "cpuRate":"2.05" } ] } //测试类 String jsonStr = "上面的json"; JSONObject jsonObj = JSONObject.parseObject(jsonStr); Map<String, Object> flatMap = JsonFlattener.flattenAsMap(jsonObj.toString()); //将 . 改成 , //Map<String, Object> flatMap = new JsonFlattener(jsonObj.toString()).withSeparator(',').flattenAsMap(); for (Map.Entry<String, Object> entry : flatMap.entrySet()) { System.out.println(entry.getKey() + " : " + entry.getValue()); } //效果 ip : 192.168.100.100 index : 3 type : 0 value[0].cpuCores : 8 value[0].cpuIndex : 0 value[0].cpuRate : 0.85 value[0].cpuFreq : 2.10GHz value[0].cpuName : Intel(R) Xeon(R) CPUE5-2620 v4 value[1].cpuCores : 8 value[1].cpuIndex : 1 value[1].cpuRate : 2.05 value[1].cpuFreq : 2.10GHz value[1].cpuName : Intel(R) Xeon(R) CPUE5-2620 v4 timestamp : 1555317936 #-------------------------------------------------------------------------------- ip : 192.168.100.100 index : 3 type : 0 value[0],cpuCores : 8 value[0],cpuIndex : 0 value[0],cpuRate : 0.85 value[0],cpuFreq : 2.10GHz value[0],cpuName : Intel(R) Xeon(R) CPUE5-2620 v4 value[1],cpuCores : 8 value[1],cpuIndex : 1 value[1],cpuRate : 2.05 value[1],cpuFreq : 2.10GHz value[1],cpuName : Intel(R) Xeon(R) CPUE5-2620 v4 timestamp : 1555317936
二、去扁平化
//json { "registro.status": "1", "registro.nome": "SUELI", "registro.sobrenome": "BERNARDO BRITO", "registro.telefone": "1100128500", "registro.endereco": "RUA SIVI", "registro.numero": "20", "registro.complemento": "CS FR", "registro.bairro": "CIDADE INDUSTRIAL SATELITE DE SAO PAULO", "registro.cep": "07222190", "registro.cidade": "GUARULHOS", "registro.uf": "SP", "registro.cpfcnpj": "12345678900", "registro.mae": "TERESINHA", "registro.sobrenomemae": "DE FATIMA BERNARDO", "registro.nasc": "1995-08-05", "registro.protocolo": "320978777" } //测试代码 String unflattenJson = JsonUnflattener.unflatten(jsonStr); //结果 { "registro": { "status": "1", "nome": "SUELI", "sobrenome": "BERNARDO BRITO", "telefone": "1100128500", "endereco": "RUA SIVI", "numero": "20", "complemento": "CS FR", "bairro": "CIDADE INDUSTRIAL SATELITE DE SAO PAULO", "cep": "07222190", "cidade": "GUARULHOS", "uf": "SP", "cpfcnpj": "12345678900", "mae": "TERESINHA", "sobrenomemae": "DE FATIMA BERNARDO", "nasc": "1995-08-05", "protocolo": "320978777" } }
参考:
1. https://blog.csdn.net/u012510831/article/details/104954425
2. https://blog.csdn.net/jx2956/article/details/86700686
持续更新!!!