<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.chenwc</groupId> <artifactId>AnalysisJson</artifactId> <version>1.0-SNAPSHOT</version> <properties> <slf4j.version>2.0.5</slf4j.version> <log4j.version>1.2.17</log4j.version> <maven.compiler.plugin.version>3.6.1</maven.compiler.plugin.version> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.83</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.12.0</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-reload4j</artifactId> <version>${slf4j.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${maven.compiler.plugin.version}</version> </plugin> </plugins> </build> </project>
log4j.rootLogger=info,stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d %-5p %-5L --- [%-5t] %-10c : %m %n
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * @author chenwc * @date 2023/11/13 20:48 */ public class AnalysisJson { private static final Logger log = LoggerFactory.getLogger(AnalysisJson.class); public static void main(String[] args) { // TODO 自动生成的方法存根 JSONObject jsonObject = JSONObject.parseObject("{\"batchId\":\"1260219663439168\",\"items\":[{\"GYSC\":[],\"YHXX\":[],\"KPXX\":[{\"INVOICE_NUM\":\"2121\",\"CUSBUKRS\":\"0150\",\"POST_ACCOUNT_CUS\":\"\",\"INVOICE_NAME\":\"浙江东廷物产集团有限公司01\",\"CUSBUKRS_FLAG\":\"\",\"INVOICE_ADDR\":\"\",\"INVOICE_TEL\":\"\",\"CUS_TYPE\":\"贸易商\",\"INVOICE_BANK\":\"2121\",\"NAME\":\"浙江东廷物产集团有限公司\"},{\"INVOICE_NUM\":\"41241\",\"CUSBUKRS\":\"0150\",\"POST_ACCOUNT_CUS\":\"\",\"INVOICE_NAME\":\"浙江东廷物产集团有限公司02\",\"CUSBUKRS_FLAG\":\"\",\"INVOICE_ADDR\":\"\",\"INVOICE_TEL\":\"\",\"CUS_TYPE\":\"贸易商\",\"INVOICE_BANK\":\"1241\",\"NAME\":\"浙江东廷物产集团有限公司\"},{\"INVOICE_NUM\":\"12421\",\"CUSBUKRS\":\"无\",\"POST_ACCOUNT_CUS\":\"\",\"INVOICE_NAME\":\"浙江东廷物产集团有限公司2-22\",\"CUSBUKRS_FLAG\":\"\",\"INVOICE_ADDR\":\"\",\"INVOICE_TEL\":\"\",\"CUS_TYPE\":\"贸易商\",\"INVOICE_BANK\":\"321412\",\"NAME\":\"浙江东廷物产集团有限公司\"},{\"INVOICE_NUM\":\"423423\",\"CUSBUKRS\":\"无\",\"POST_ACCOUNT_CUS\":\"\",\"INVOICE_NAME\":\"浙江东廷物产集团有限公司2-23\",\"CUSBUKRS_FLAG\":\"\",\"INVOICE_ADDR\":\"\",\"INVOICE_TEL\":\"\",\"CUS_TYPE\":\"贸易商\",\"INVOICE_BANK\":\"4343243\",\"NAME\":\"浙江东廷物产集团有限公司\"}],\"KHC\":[],\"BP\":{\"TAXPAYER\":\"\",\"TXTLG\":\"浙江东廷物产集团有限公司\",\"PARTNER_ORGTYPE\":\"1\",\"COM_PROV\":\"330000\",\"BP_ID_CODE\":\"\",\"COM_AREA\":\"\",\"COUNTRY\":\"CN\",\"BP_SOCIAL_NUM\":\"\",\"BU_GROUP\":\"Z002\",\"BP_INDSCT\":\"F5135\",\"EMPNO\":\"\",\"COM_TYPE\":\"有限责任公司(自然人投资或控股)\",\"ZNAME_EN\":\"\",\"COM_TEL\":\"\",\"TRADE_TYPE\":\"1,2\",\"TXTSH\":\"\",\"BP_ID_CARD\":\"\",\"CODE\":\"1003000361\",\"COM_STATUS\":\"1\",\"ZFDDBR\":\"施政平\",\"COM_CITY\":\"\",\"FOUND_DAT\":\"2017-04-17\",\"LASTUPDATEDTIME\":\"2023-11-15 17:44:34\",\"VERSION\":4,\"PARTNER_TYPE\":\"\",\"STRAS\":\"浙江省杭州市上城区财富金融中心2幢3504室\",\"BP_ID_NUM\":\"91330104MA28NWF65N\"}}]}"); try { JSONObject recvJsonObject = new JSONObject(true); recursionJsonObject("", jsonObject, recvJsonObject); log.info("转换后的 JSON 对象: {}", recvJsonObject.toJSONString()); for (String key : recvJsonObject.keySet()) { log.info("路径: {},值: {}", key, recvJsonObject.getString(key)); } } catch (Exception e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } /** * 递归json对象 * * @param path json路径 * @param json 待打印路径的json对象 * @param recv 接收json路径的json对象 */ public static void recursionJsonObject(String path, JSONObject json, JSONObject recv) throws Exception { for (String key : json.keySet()) { Object object = json.get(key); // JSON对象 if (object instanceof JSONObject) { String newPath; if (StringUtils.isNotEmpty(path)) { newPath = path + "." + key; } else { newPath = key; } recursionJsonObject(newPath, (JSONObject) object, recv); } // JSON数组 else if (object instanceof JSONArray) { String newPath; if (StringUtils.isNotEmpty(path)) { newPath = path + "." + key; } else { newPath = key; } JSONArray jsonArray = json.getJSONArray(key); if (null != jsonArray && jsonArray.size() > 0) { recursionJsonArray(newPath, json.getJSONArray(key), recv); } else { if (StringUtils.isEmpty(path)) { recv.put(key, object); } else { recv.put(path + "." + key, object); } } } // 值 else { if (StringUtils.isEmpty(path)) { recv.put(key, object); } else { recv.put(path + "." + key, object); } } } } /** * 递归json数组 * * @param path json路径 * @param jsonArray json数组 * @param recv 接收json路径的json对象 */ public static void recursionJsonArray(String path, JSONArray jsonArray, JSONObject recv) throws Exception { int i = 0; for (Object object : jsonArray) { // JSON对象 if (object instanceof JSONObject) { recursionJsonObject(path + "[" + i + "]", (JSONObject) object, recv); } // JSON数组 else if (object instanceof JSONArray) { recursionJsonArray(path + "[" + i + "]", (JSONArray) object, recv); } i++; } } }
输入:
{"batchId":"1260219663439168","items":[{"GYSC":[],"YHXX":[],"KPXX":[{"INVOICE_NUM":"2121","CUSBUKRS":"0150","POST_ACCOUNT_CUS":"","INVOICE_NAME":"浙江东廷物产集团有限公司01","CUSBUKRS_FLAG":"","INVOICE_ADDR":"","INVOICE_TEL":"","CUS_TYPE":"贸易商","INVOICE_BANK":"2121","NAME":"浙江东廷物产集团有限公司"},{"INVOICE_NUM":"41241","CUSBUKRS":"0150","POST_ACCOUNT_CUS":"","INVOICE_NAME":"浙江东廷物产集团有限公司02","CUSBUKRS_FLAG":"","INVOICE_ADDR":"","INVOICE_TEL":"","CUS_TYPE":"贸易商","INVOICE_BANK":"1241","NAME":"浙江东廷物产集团有限公司"},{"INVOICE_NUM":"12421","CUSBUKRS":"无","POST_ACCOUNT_CUS":"","INVOICE_NAME":"浙江东廷物产集团有限公司2-22","CUSBUKRS_FLAG":"","INVOICE_ADDR":"","INVOICE_TEL":"","CUS_TYPE":"贸易商","INVOICE_BANK":"321412","NAME":"浙江东廷物产集团有限公司"},{"INVOICE_NUM":"423423","CUSBUKRS":"无","POST_ACCOUNT_CUS":"","INVOICE_NAME":"浙江东廷物产集团有限公司2-23","CUSBUKRS_FLAG":"","INVOICE_ADDR":"","INVOICE_TEL":"","CUS_TYPE":"贸易商","INVOICE_BANK":"4343243","NAME":"浙江东廷物产集团有限公司"}],"KHC":[],"BP":{"TAXPAYER":"","TXTLG":"浙江东廷物产集团有限公司","PARTNER_ORGTYPE":"1","COM_PROV":"330000","BP_ID_CODE":"","COM_AREA":"","COUNTRY":"CN","BP_SOCIAL_NUM":"","BU_GROUP":"Z002","BP_INDSCT":"F5135","EMPNO":"","COM_TYPE":"有限责任公司(自然人投资或控股)","ZNAME_EN":"","COM_TEL":"","TRADE_TYPE":"1,2","TXTSH":"","BP_ID_CARD":"","CODE":"1003000361","COM_STATUS":"1","ZFDDBR":"施政平","COM_CITY":"","FOUND_DAT":"2017-04-17","LASTUPDATEDTIME":"2023-11-15 17:44:34","VERSION":4,"PARTNER_TYPE":"","STRAS":"浙江省杭州市上城区财富金融中心2幢3504室","BP_ID_NUM":"91330104MA28NWF65N"}}]}
输出:
{"batchId":"1260219663439168","items[0].GYSC":[],"items[0].YHXX":[],"items[0].KPXX[0].INVOICE_NUM":"2121","items[0].KPXX[0].CUSBUKRS":"0150","items[0].KPXX[0].POST_ACCOUNT_CUS":"","items[0].KPXX[0].INVOICE_NAME":"浙江东廷物产集团有限公司01","items[0].KPXX[0].CUSBUKRS_FLAG":"","items[0].KPXX[0].INVOICE_ADDR":"","items[0].KPXX[0].INVOICE_TEL":"","items[0].KPXX[0].CUS_TYPE":"贸易商","items[0].KPXX[0].INVOICE_BANK":"2121","items[0].KPXX[0].NAME":"浙江东廷物产集团有限公司","items[0].KPXX[1].INVOICE_NUM":"41241","items[0].KPXX[1].CUSBUKRS":"0150","items[0].KPXX[1].POST_ACCOUNT_CUS":"","items[0].KPXX[1].INVOICE_NAME":"浙江东廷物产集团有限公司02","items[0].KPXX[1].CUSBUKRS_FLAG":"","items[0].KPXX[1].INVOICE_ADDR":"","items[0].KPXX[1].INVOICE_TEL":"","items[0].KPXX[1].CUS_TYPE":"贸易商","items[0].KPXX[1].INVOICE_BANK":"1241","items[0].KPXX[1].NAME":"浙江东廷物产集团有限公司","items[0].KPXX[2].INVOICE_NUM":"12421","items[0].KPXX[2].CUSBUKRS":"无","items[0].KPXX[2].POST_ACCOUNT_CUS":"","items[0].KPXX[2].INVOICE_NAME":"浙江东廷物产集团有限公司2-22","items[0].KPXX[2].CUSBUKRS_FLAG":"","items[0].KPXX[2].INVOICE_ADDR":"","items[0].KPXX[2].INVOICE_TEL":"","items[0].KPXX[2].CUS_TYPE":"贸易商","items[0].KPXX[2].INVOICE_BANK":"321412","items[0].KPXX[2].NAME":"浙江东廷物产集团有限公司","items[0].KPXX[3].INVOICE_NUM":"423423","items[0].KPXX[3].CUSBUKRS":"无","items[0].KPXX[3].POST_ACCOUNT_CUS":"","items[0].KPXX[3].INVOICE_NAME":"浙江东廷物产集团有限公司2-23","items[0].KPXX[3].CUSBUKRS_FLAG":"","items[0].KPXX[3].INVOICE_ADDR":"","items[0].KPXX[3].INVOICE_TEL":"","items[0].KPXX[3].CUS_TYPE":"贸易商","items[0].KPXX[3].INVOICE_BANK":"4343243","items[0].KPXX[3].NAME":"浙江东廷物产集团有限公司","items[0].KHC":[],"items[0].BP.TAXPAYER":"","items[0].BP.TXTLG":"浙江东廷物产集团有限公司","items[0].BP.PARTNER_ORGTYPE":"1","items[0].BP.COM_PROV":"330000","items[0].BP.BP_ID_CODE":"","items[0].BP.COM_AREA":"","items[0].BP.COUNTRY":"CN","items[0].BP.BP_SOCIAL_NUM":"","items[0].BP.BU_GROUP":"Z002","items[0].BP.BP_INDSCT":"F5135","items[0].BP.EMPNO":"","items[0].BP.COM_TYPE":"有限责任公司(自然人投资或控股)","items[0].BP.ZNAME_EN":"","items[0].BP.COM_TEL":"","items[0].BP.TRADE_TYPE":"1,2","items[0].BP.TXTSH":"","items[0].BP.BP_ID_CARD":"","items[0].BP.CODE":"1003000361","items[0].BP.COM_STATUS":"1","items[0].BP.ZFDDBR":"施政平","items[0].BP.COM_CITY":"","items[0].BP.FOUND_DAT":"2017-04-17","items[0].BP.LASTUPDATEDTIME":"2023-11-15 17:44:34","items[0].BP.VERSION":4,"items[0].BP.PARTNER_TYPE":"","items[0].BP.STRAS":"浙江省杭州市上城区财富金融中心2幢3504室","items[0].BP.BP_ID_NUM":"91330104MA28NWF65N"}