java解析json复杂数据的第三种思路

发布时间:2024年01月09日

一、概述

接上篇 java解析json复杂数据的两种思路
我们已经通过解析返回json字符串得到数据,现在改变思路, 按照如下流程获取数据:

接口API获取JSON
JSON转XML
dom4j使用XPath解析xml

二、数据预览

1. 接口json数据

https://blog.csdn.net/community/home-api/v1/get-business-list?page=1&size=5&businessType=blog&username=qq_16127313

{
    "code":200,
    "message":"success",
    "traceId":"cec7dfb0-78f4-40c4-b1ce-7463b100e3fd",
    "data":{
        "list":[
            {
                "articleId":135418954,
                "title":"java解析json复杂数据的两种思路",
                "description":"萌新小明最近新开了CSDN博客,蠢蠢欲动,迫不及待的发表了几篇工作中积累下来的解决问题的涂鸦之作,看着访问量慢慢涨起来,心中暗暗窃喜。现在小明想每天23点记录一下每篇文章的访问量。。。",
                "url":"https://blog.csdn.net/qq_16127313/article/details/135418954",
                "type":1,
                "top":false,
                "forcePlan":false,
                "viewCount":947,
                "commentCount":0,
                "editUrl":"https://editor.csdn.net/md?articleId=135418954",
                "postTime":"2024-01-06 14:11:40",
                "diggCount":16,
                "formatTime":"前天 14:11",
                "picList":[
                    "https://img-blog.csdnimg.cn/direct/a409a0f4555c459fa05c00fd9ee0ea8c.png"
                ],
                "collectCount":21
            },
            {
                "articleId":135244727,
                "title":"java lambda表达式训练题一",
                "description":"Lambda 表达式,也可称为闭包,它是推动 Java 8 发布的最重要新特性。Lambda 允许把函数作为一个方法的参数(函数作为参数传递进方法中)。使用 Lambda 表达式可以使代码变的更加简洁紧凑。",
                "url":"https://blog.csdn.net/qq_16127313/article/details/135244727",
                "type":1,
                "top":false,
                "forcePlan":false,
                "viewCount":1238,
                "commentCount":0,
                "editUrl":"https://editor.csdn.net/md?articleId=135244727",
                "postTime":"2023-12-27 18:07:30",
                "diggCount":7,
                "formatTime":"2023.12.27",
                "picList":[
                    "https://img-blog.csdnimg.cn/direct/d59c68b950754e879914b5319cd1b53f.png"
                ],
                "collectCount":8
            },
            {
                "articleId":135173565,
                "title":"二维码初体验 com.google.zxing 实现续 - web api封装",
                "description":"在 二维码初体验 com.google.zxing 实现 我们实现了二维码的生成,但是大部分情况下,二维码的相关功能是作为API接口来提供服务的。我们下面便演示在springboot、Knife4j下封装api接口来实现二维码生成功能。如何使用下面的备份文件恢复成原始的项目代码,请移步查阅:神奇代码恢复工具-over-",
                "url":"https://blog.csdn.net/qq_16127313/article/details/135173565",
                "type":1,
                "top":false,
                "forcePlan":false,
                "viewCount":1904,
                "commentCount":0,
                "editUrl":"https://editor.csdn.net/md?articleId=135173565",
                "postTime":"2023-12-23 20:17:11",
                "diggCount":23,
                "formatTime":"2023.12.23",
                "picList":[
                    "https://img-blog.csdnimg.cn/direct/f0c994ca789a495a8c8c03d86d626f24.jpeg"
                ],
                "collectCount":23
            },
            {
                "articleId":135167613,
                "title":"二维码初体验 com.google.zxing 实现",
                "description":"Java 操作二维码的开源项目很多,如 SwetakeQRCode、BarCode4j、Zxing 等,这边以Zxing 为例进行介绍。选择需要生成QR原始文件,支持 “清除空白行及空格” 以减少二维码图片大小。支持输入文本内容,直接生成二维码代码结构QrCodeUI: 完整版本代码SimpleQrCodeUI:简化版本代码如何使用下面的备份文件恢复成原始的项目代码,请移步查阅:神奇代码恢复工具-over-",
                "url":"https://blog.csdn.net/qq_16127313/article/details/135167613",
                "type":1,
                "top":false,
                "forcePlan":false,
                "viewCount":1286,
                "commentCount":0,
                "editUrl":"https://editor.csdn.net/md?articleId=135167613",
                "postTime":"2023-12-23 13:52:23",
                "diggCount":6,
                "formatTime":"2023.12.23",
                "picList":[
                    "https://img-blog.csdnimg.cn/direct/d3eeac85857543869dce8967c570bdc4.jpeg"
                ],
                "collectCount":11
            },
            {
                "articleId":135135799,
                "title":"【随笔】MD5加密字符串、文件apache、springframework实现",
                "description":"【代码】【随笔】MD5加密字符串、文件commons-codec、springframework实现。",
                "url":"https://blog.csdn.net/qq_16127313/article/details/135135799",
                "type":1,
                "top":false,
                "forcePlan":false,
                "viewCount":1721,
                "commentCount":0,
                "editUrl":"https://editor.csdn.net/md?articleId=135135799",
                "postTime":"2023-12-21 17:29:54",
                "diggCount":9,
                "formatTime":"2023.12.21",
                "picList":[
                    "https://img-blog.csdnimg.cn/direct/dc26b7f1c731494f80c8c3b3badfa95d.jpeg"
                ],
                "collectCount":9
            }
        ],
        "total":72
    }
}

2. json转xml数据

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <code>200</code>
  <message>success</message>
  <traceId>cec7dfb0-78f4-40c4-b1ce-7463b100e3fd</traceId>
  <data>
    <list>
      <articleId>135418954</articleId>
      <title>java解析json复杂数据的两种思路</title>
      <description>萌新小明最近新开了CSDN博客,蠢蠢欲动,迫不及待的发表了几篇工作中积累下来的解决问题的涂鸦之作,看着访问量慢慢涨起来,心中暗暗窃喜。现在小明想每天23点记录一下每篇文章的访问量。。。</description>
      <url>https://blog.csdn.net/qq_16127313/article/details/135418954</url>
      <type>1</type>
      <top>false</top>
      <forcePlan>false</forcePlan>
      <viewCount>947</viewCount>
      <commentCount>0</commentCount>
      <editUrl>https://editor.csdn.net/md?articleId=135418954</editUrl>
      <postTime>2024-01-06 14:11:40</postTime>
      <diggCount>16</diggCount>
      <formatTime>前天 14:11</formatTime>
      <picList>https://img-blog.csdnimg.cn/direct/a409a0f4555c459fa05c00fd9ee0ea8c.png</picList>
      <collectCount>21</collectCount>
    </list>
    <list>
      <articleId>135244727</articleId>
      <title>java lambda表达式训练题一</title>
      <description>Lambda 表达式,也可称为闭包,它是推动 Java 8 发布的最重要新特性。Lambda 允许把函数作为一个方法的参数(函数作为参数传递进方法中)。使用 Lambda 表达式可以使代码变的更加简洁紧凑。</description>
      <url>https://blog.csdn.net/qq_16127313/article/details/135244727</url>
      <type>1</type>
      <top>false</top>
      <forcePlan>false</forcePlan>
      <viewCount>1238</viewCount>
      <commentCount>0</commentCount>
      <editUrl>https://editor.csdn.net/md?articleId=135244727</editUrl>
      <postTime>2023-12-27 18:07:30</postTime>
      <diggCount>7</diggCount>
      <formatTime>2023.12.27</formatTime>
      <picList>https://img-blog.csdnimg.cn/direct/d59c68b950754e879914b5319cd1b53f.png</picList>
      <collectCount>8</collectCount>
    </list>
    <list>
      <articleId>135173565</articleId>
      <title>二维码初体验 com.google.zxing 实现续 - web api封装</title>
      <description>在 二维码初体验 com.google.zxing 实现 我们实现了二维码的生成,但是大部分情况下,二维码的相关功能是作为API接口来提供服务的。我们下面便演示在springboot、Knife4j下封装api接口来实现二维码生成功能。如何使用下面的备份文件恢复成原始的项目代码,请移步查阅:神奇代码恢复工具-over-</description>
      <url>https://blog.csdn.net/qq_16127313/article/details/135173565</url>
      <type>1</type>
      <top>false</top>
      <forcePlan>false</forcePlan>
      <viewCount>1904</viewCount>
      <commentCount>0</commentCount>
      <editUrl>https://editor.csdn.net/md?articleId=135173565</editUrl>
      <postTime>2023-12-23 20:17:11</postTime>
      <diggCount>23</diggCount>
      <formatTime>2023.12.23</formatTime>
      <picList>https://img-blog.csdnimg.cn/direct/f0c994ca789a495a8c8c03d86d626f24.jpeg</picList>
      <collectCount>23</collectCount>
    </list>
    <list>
      <articleId>135167613</articleId>
      <title>二维码初体验 com.google.zxing 实现</title>
      <description>Java 操作二维码的开源项目很多,如 SwetakeQRCode、BarCode4j、Zxing 等,这边以Zxing 为例进行介绍。选择需要生成QR原始文件,支持 “清除空白行及空格” 以减少二维码图片大小。支持输入文本内容,直接生成二维码代码结构QrCodeUI: 完整版本代码SimpleQrCodeUI:简化版本代码如何使用下面的备份文件恢复成原始的项目代码,请移步查阅:神奇代码恢复工具-over-</description>
      <url>https://blog.csdn.net/qq_16127313/article/details/135167613</url>
      <type>1</type>
      <top>false</top>
      <forcePlan>false</forcePlan>
      <viewCount>1286</viewCount>
      <commentCount>0</commentCount>
      <editUrl>https://editor.csdn.net/md?articleId=135167613</editUrl>
      <postTime>2023-12-23 13:52:23</postTime>
      <diggCount>6</diggCount>
      <formatTime>2023.12.23</formatTime>
      <picList>https://img-blog.csdnimg.cn/direct/d3eeac85857543869dce8967c570bdc4.jpeg</picList>
      <collectCount>11</collectCount>
    </list>
    <list>
      <articleId>135135799</articleId>
      <title>【随笔】MD5加密字符串、文件apache、springframework实现</title>
      <description>【代码】【随笔】MD5加密字符串、文件commons-codec、springframework实现。</description>
      <url>https://blog.csdn.net/qq_16127313/article/details/135135799</url>
      <type>1</type>
      <top>false</top>
      <forcePlan>false</forcePlan>
      <viewCount>1721</viewCount>
      <commentCount>0</commentCount>
      <editUrl>https://editor.csdn.net/md?articleId=135135799</editUrl>
      <postTime>2023-12-21 17:29:54</postTime>
      <diggCount>9</diggCount>
      <formatTime>2023.12.21</formatTime>
      <picList>https://img-blog.csdnimg.cn/direct/dc26b7f1c731494f80c8c3b3badfa95d.jpeg</picList>
      <collectCount>9</collectCount>
    </list>
    <total>72</total>
  </data>
</root>


三、代码实现

1. pom.xml


		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webflux</artifactId>
			<version>5.2.3.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>io.projectreactor.netty</groupId>
			<artifactId>reactor-netty</artifactId>
			<version>0.9.4.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-slf4j-impl</artifactId>
			<version>2.12.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.10</version>
		</dependency>
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.5</version>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.dataformat</groupId>
			<artifactId>jackson-dataformat-xml</artifactId>
			<version>2.10.2</version>
		</dependency>
		<dependency>
		    <groupId>org.dom4j</groupId>
		    <artifactId>dom4j</artifactId>
		    <version>2.1.4</version>
		</dependency>
		<dependency>
		    <groupId>jaxen</groupId>
		    <artifactId>jaxen</artifactId>
		    <version>2.0.0</version>
		</dependency>
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-api</artifactId>
			<version>5.5.2</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.18.12</version>
			<scope>provided</scope>
		</dependency>

2. 核心代码

package com.fly.xml;

import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;

import org.apache.commons.io.IOUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.client.WebClient;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class XmlTest
{
    static JsonMapper jsonMapper = new JsonMapper();
    
    static XmlMapper xmlMapper = new XmlMapper();
    
    static WebClient webClient = WebClient.builder().codecs(configurer -> configurer.defaultCodecs().maxInMemorySize(-1)).build(); 
    
   
    @Test
    public void testApi()
        throws IOException, DocumentException
    {
        //WebClient同步调用
        String url = "https://blog.csdn.net/community/home-api/v1/get-business-list?page=1&size=5&businessType=blog&username=qq_16127313";
        String jsonText = webClient.get().uri(url).acceptCharset(StandardCharsets.UTF_8).accept(MediaType.APPLICATION_JSON).retrieve().bodyToMono(String.class).block();
       
        //json转xml
        JsonNode jsonNode = jsonMapper.readTree(jsonText);
        String xml = xmlMapper.writeValueAsString(jsonNode);
        
        // dom4j、XPath
        Document doc = new SAXReader().read(new StringReader(xml));
        doc.selectNodes("//data/list/url").stream().forEach(n -> log.info(n.getStringValue()));
        doc.selectNodes("//data/list/viewCount").stream().forEach(n -> log.info(n.getStringValue()));
    }
}

3. 运行结果

在这里插入图片描述

四、源码传送

https://gitee.com/00fly/effict-side/blob/master/springboot-cache/src/test/java/com/fly/test/restful/json/ParseJson.java


大家可以根据需要选择方案,有任何问题和建议,都可以向我提问讨论,大家一起进步,谢谢!

-over-

文章来源:https://blog.csdn.net/qq_16127313/article/details/135469157
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。