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 node
            JsonNode bodyNode = rootJsonNode.get("body");
            //第二层的body attribute
            String 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 node
            JsonNode couponsNode = bodyNode.get("coupons");
            for(int i = 0;i<couponsNode.size();i++){
                //第三层的couponsNode 的 coupon node
                JsonNode couponNode = couponsNode.get(i);
                //第三层的couponNode 的 attribute
                String 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 的 couponCodeNode
                JsonNode couponCodeNode = couponNode.get("couponCode");
                for(int j = 0;j<couponCodeNode.size();j++){
                    //第四层的couponCodeNode 的 couponCode node
                    JsonNode couponCode = couponCodeNode.get(j);
                    //第四层的couponCode 的 couponCode node
                    int 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();
    }
}
相关推荐
s***4698几秒前
【SpringBoot篇】详解Bean的管理(获取bean,bean的作用域,第三方bean)
java·spring boot·后端
AntBlack6 分钟前
Z-Image 发布了 ,赶紧体验了一把(配套 Modal执行脚本)
前端·后端·aigc
诸葛韩信36 分钟前
前端工程化1——npm insatall背后的工作原理
前端·npm·node.js
动亦定42 分钟前
页面导出大量数据导致响应超时解决方案
java·mysql
k***12171 小时前
SpringBoot返回文件让前端下载的几种方式
前端·spring boot·后端
专注前端30年1 小时前
如何使用 HTML5 的 Canvas + JavaScript 实现炫酷的游戏得分特效?
前端·javascript·游戏·html5·canvas·canva可画
q***06291 小时前
解决 Tomcat 跨域问题 - Tomcat 配置静态文件和 Java Web 服务(Spring MVC Springboot)同时允许跨域
java·前端·spring
还是鼠鼠1 小时前
Redisson实现的分布式锁能解决主从一致性的问题吗?
java·数据库·redis·分布式·缓存·面试·redisson
d***95621 小时前
windows配置永久路由
java
木易 士心2 小时前
深入理解 CSS 中的 !important
前端·css