java實(shí)現(xiàn)解析json復(fù)雜數(shù)據(jù)的第三種思路詳解
一、概述
接上篇 java解析json復(fù)雜數(shù)據(jù)的兩種思路
我們已經(jīng)通過解析返回json字符串得到數(shù)據(jù),現(xiàn)在改變思路, 按照如下流程獲取數(shù)據(jù):
二、數(shù)據(jù)預(yù)覽
1. 接口json數(shù)據(jù)
{ "code":200, "message":"success", "traceId":"cec7dfb0-78f4-40c4-b1ce-7463b100e3fd", "data":{ "list":[ { "articleId":135418954, "title":"java解析json復(fù)雜數(shù)據(jù)的兩種思路", "description":"萌新小明最近新開了CSDN博客,蠢蠢欲動(dòng),迫不及待的發(fā)表了幾篇工作中積累下來的解決問題的涂鴉之作,看著訪問量慢慢漲起來,心中暗暗竊喜。現(xiàn)在小明想每天23點(diǎn)記錄一下每篇文章的訪問量。。。", "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表達(dá)式訓(xùn)練題一", "description":"Lambda 表達(dá)式,也可稱為閉包,它是推動(dòng) Java 8 發(fā)布的最重要新特性。Lambda 允許把函數(shù)作為一個(gè)方法的參數(shù)(函數(shù)作為參數(shù)傳遞進(jìn)方法中)。使用 Lambda 表達(dá)式可以使代碼變的更加簡潔緊湊。", "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":"二維碼初體驗(yàn) com.google.zxing 實(shí)現(xiàn)續(xù) - web api封裝", "description":"在 二維碼初體驗(yàn) com.google.zxing 實(shí)現(xiàn) 我們實(shí)現(xiàn)了二維碼的生成,但是大部分情況下,二維碼的相關(guān)功能是作為API接口來提供服務(wù)的。我們下面便演示在springboot、Knife4j下封裝api接口來實(shí)現(xiàn)二維碼生成功能。如何使用下面的備份文件恢復(fù)成原始的項(xiàng)目代碼,請(qǐng)移步查閱:神奇代碼恢復(fù)工具-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":"二維碼初體驗(yàn) com.google.zxing 實(shí)現(xiàn)", "description":"Java 操作二維碼的開源項(xiàng)目很多,如 SwetakeQRCode、BarCode4j、Zxing 等,這邊以Zxing 為例進(jìn)行介紹。選擇需要生成QR原始文件,支持 “清除空白行及空格” 以減少二維碼圖片大小。支持輸入文本內(nèi)容,直接生成二維碼代碼結(jié)構(gòu)QrCodeUI: 完整版本代碼SimpleQrCodeUI:簡化版本代碼如何使用下面的備份文件恢復(fù)成原始的項(xiàng)目代碼,請(qǐng)移步查閱:神奇代碼恢復(fù)工具-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實(shí)現(xiàn)", "description":"【代碼】【隨筆】MD5加密字符串、文件commons-codec、springframework實(shí)現(xiàn)。", "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轉(zhuǎn)xml數(shù)據(jù)
<?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復(fù)雜數(shù)據(jù)的兩種思路</title> <description>萌新小明最近新開了CSDN博客,蠢蠢欲動(dòng),迫不及待的發(fā)表了幾篇工作中積累下來的解決問題的涂鴉之作,看著訪問量慢慢漲起來,心中暗暗竊喜?,F(xiàn)在小明想每天23點(diǎn)記錄一下每篇文章的訪問量。。。</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表達(dá)式訓(xùn)練題一</title> <description>Lambda 表達(dá)式,也可稱為閉包,它是推動(dòng) Java 8 發(fā)布的最重要新特性。Lambda 允許把函數(shù)作為一個(gè)方法的參數(shù)(函數(shù)作為參數(shù)傳遞進(jìn)方法中)。使用 Lambda 表達(dá)式可以使代碼變的更加簡潔緊湊。</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>二維碼初體驗(yàn) com.google.zxing 實(shí)現(xiàn)續(xù) - web api封裝</title> <description>在 二維碼初體驗(yàn) com.google.zxing 實(shí)現(xiàn) 我們實(shí)現(xiàn)了二維碼的生成,但是大部分情況下,二維碼的相關(guān)功能是作為API接口來提供服務(wù)的。我們下面便演示在springboot、Knife4j下封裝api接口來實(shí)現(xiàn)二維碼生成功能。如何使用下面的備份文件恢復(fù)成原始的項(xiàng)目代碼,請(qǐng)移步查閱:神奇代碼恢復(fù)工具-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>二維碼初體驗(yàn) com.google.zxing 實(shí)現(xiàn)</title> <description>Java 操作二維碼的開源項(xiàng)目很多,如 SwetakeQRCode、BarCode4j、Zxing 等,這邊以Zxing 為例進(jìn)行介紹。選擇需要生成QR原始文件,支持 “清除空白行及空格” 以減少二維碼圖片大小。支持輸入文本內(nèi)容,直接生成二維碼代碼結(jié)構(gòu)QrCodeUI: 完整版本代碼SimpleQrCodeUI:簡化版本代碼如何使用下面的備份文件恢復(fù)成原始的項(xiàng)目代碼,請(qǐng)移步查閱:神奇代碼恢復(fù)工具-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實(shí)現(xiàn)</title> <description>【代碼】【隨筆】MD5加密字符串、文件commons-codec、springframework實(shí)現(xiàn)。</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>
三、代碼實(shí)現(xiàn)
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同步調(diào)用 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轉(zhuǎn)xml JsonNode jsonNode = jsonMapper.readTree(jsonText); String xml = xmlMapper.writeValueAsString(jsonNode); // dom4j、XPath Document doc = new SAXReader().read(new StringReader(xml)); doc.selectNodes("http://data/list/url").stream().forEach(n -> log.info(n.getStringValue())); doc.selectNodes("http://data/list/viewCount").stream().forEach(n -> log.info(n.getStringValue())); } }
3. 運(yùn)行結(jié)果
四、源碼傳送
以上就是java實(shí)現(xiàn)解析json復(fù)雜數(shù)據(jù)的第三種思路詳解的詳細(xì)內(nèi)容,更多關(guān)于java解析json復(fù)雜數(shù)據(jù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
java unicode轉(zhuǎn)碼為中文實(shí)例
這篇文章主要介紹了java unicode轉(zhuǎn)碼為中文的實(shí)例,大家參考使用吧2013-12-12Java中BigDecimal類與int、Integer使用總結(jié)
這篇文章主要給大家介紹了關(guān)于Java中BigDecimal類與int、Integer使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07Filter、Servlet、Listener的學(xué)習(xí)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Filter、Servlet、Listener的學(xué)習(xí)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07SpringBoot項(xiàng)目中使用Swagger2及注解解釋的詳細(xì)教程
Swagger2是一個(gè)開源項(xiàng)目,用于為RESTful Web服務(wù)生成REST API文檔,下面這篇文章主要給大家介紹了關(guān)于SpringBoot項(xiàng)目中使用Swagger2及注解解釋的詳細(xì)教程,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04Java 互相關(guān)聯(lián)的實(shí)體無限遞歸問題的解決
這篇文章主要介紹了Java 互相關(guān)聯(lián)的實(shí)體無限遞歸問題的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10