欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 名人名企 > java解析复杂json

java解析复杂json

2025/2/8 0:06:10 来源:https://blog.csdn.net/weixin_43103956/article/details/145467750  浏览:    关键词:java解析复杂json

我个人验证了两个方法
一.基于org.json

1.jar包依赖

<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency><groupId>org.json</groupId><artifactId>json</artifactId><version>20240303</version>
</dependency>

2.test json,精简后的测试json

level1Array: [{"couponId": "DBD6E1A48CC699C942618BE41CA231DA","title": "11.9元麦辣鸡腿堡","couponCode": [{"couponCodeEndTime": "2024-12-23 23:59:59","couponCardType": 0}],"remainQuantity": 29455158,"promotionId": "111799015","receiveQuantity": 1
}, {"couponId": "D8989417A8CBE902E55D96CED8B81C54","title": "姜姜暖奶铁买一送一","couponCode": [{"couponCodeEndTime": "2024-12-27 23:59:59","couponCardType": 0}],"remainQuantity": 1720778,"promotionId": "111756005","receiveQuantity": 1
}]

3.解析源码,代码都是验证过的

/**jackson解析*/void paserJsonByJackson(){try {String jsonContent = new String(Files.readAllBytes(Paths.get("D://1/coujsontest.json")));ObjectMapper objectMapper = new ObjectMapper();//第一层JsonNode rootJsonNode = objectMapper.readTree(jsonContent);String msgId = rootJsonNode.get("msgId").asText();String traceId = rootJsonNode.get("traceId").asText();String from = rootJsonNode.get("from").asText();logger.info("msgId: " + msgId);logger.info("traceId: " + traceId);logger.info("from: " + from);//第一层的body nodeJsonNode bodyNode = rootJsonNode.get("body");//第二层的body attributeString customerId = bodyNode.get("customerId").asText();int receiveType = bodyNode.get("receiveType").asInt();String tradeNo = bodyNode.get("tradeNo").asText();logger.info("customerId: " + customerId);logger.info("receiveType: " + receiveType);logger.info("tradeNo: " + tradeNo);//第二层的body 的 coupons nodeJsonNode couponsNode = bodyNode.get("coupons");for(int i = 0;i<couponsNode.size();i++){//第三层的couponsNode 的 coupon nodeJsonNode couponNode = couponsNode.get(i);//第三层的couponNode 的 attributeString couponId = couponNode.get("couponId").asText();String couponTitle = couponNode.get("title").asText();String couponPromotionId = couponNode.get("promotionId").asText();int couponReceiveQuantity = couponNode.get("receiveQuantity").asInt();int couponRemainQuantity = couponNode.get("remainQuantity").asInt();logger.info("couponId: " + couponId);logger.info("couponTitle: " + couponTitle);logger.info("couponPromotionId: " + couponPromotionId);logger.info("couponReceiveQuantity: " + couponReceiveQuantity);logger.info("couponRemainQuantity: " + couponRemainQuantity);//第三层的couponNode 的 couponCodeNodeJsonNode couponCodeNode = couponNode.get("couponCode");for(int j = 0;j<couponCodeNode.size();j++){//第四层的couponCodeNode 的 couponCode nodeJsonNode couponCode = couponCodeNode.get(j);//第四层的couponCode 的 couponCode nodeint isAvailable = couponCode.get("isAvailable").asInt();int isDeleted = couponCode.get("isDeleted").asInt();String createdUser = couponCode.get("createdUser").asText();String updatedUser = couponCode.get("updatedUser").asText();String updatedDate = couponCode.get("updatedDate").asText();logger.info("isAvailable: " + isAvailable);logger.info("isDeleted: " + isDeleted);logger.info("createdUser: " + createdUser);logger.info("updatedUser: " + updatedUser);logger.info("updatedDate: " + updatedDate);}}} catch (Exception e) {e.printStackTrace();}}

二、基于jackson,spring内部解析json也是这个,最终生产上使用的这个;
1.jar依赖

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-core</artifactId><version>2.18.2</version>
</dependency><!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.18.2</version>
</dependency><!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-annotations</artifactId><version>2.18.2</version>
</dependency>

2.源码

/**jackson解析*/
void paserJsonByJacksonTwo(){String json = "{\"name\": \"John\", \"age\": 30, \"address\": {\"street\": \"123 Main St\", \"city\": \"New York\"}}";try {ObjectMapper objectMapper = new ObjectMapper();JsonNode jsonNode = objectMapper.readTree(json);String name = jsonNode.get("name").asText();int age = jsonNode.get("age").asInt();JsonNode addressNode = jsonNode.get("address");String street = addressNode.get("street").asText();String city = addressNode.get("city").asText();logger.info("Name: " + name);logger.info("Age: " + age);logger.info("Street: " + street);logger.info("City: " + city);} catch (Exception e) {e.printStackTrace();}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com