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

文章目录

  • 一、概述
  • 二、数据预览
    • 1. 接口json数据
    • 2. json转xml数据
  • 三、代码实现
    • 1. pom.xml
    • 2. 核心代码
    • 3. 运行结果
  • 四、源码传送

一、概述

接上篇 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数据


<root>
  <code>200code>
  <message>successmessage>
  <traceId>cec7dfb0-78f4-40c4-b1ce-7463b100e3fdtraceId>
  <data>
    <list>
      <articleId>135418954articleId>
      <title>java解析json复杂数据的两种思路title>
      <description>萌新小明最近新开了CSDN博客,蠢蠢欲动,迫不及待的发表了几篇工作中积累下来的解决问题的涂鸦之作,看着访问量慢慢涨起来,心中暗暗窃喜。现在小明想每天23点记录一下每篇文章的访问量。。。description>
      <url>https://blog.csdn.net/qq_16127313/article/details/135418954url>
      <type>1type>
      <top>falsetop>
      <forcePlan>falseforcePlan>
      <viewCount>947viewCount>
      <commentCount>0commentCount>
      <editUrl>https://editor.csdn.net/md?articleId=135418954editUrl>
      <postTime>2024-01-06 14:11:40postTime>
      <diggCount>16diggCount>
      <formatTime>前天 14:11formatTime>
      <picList>https://img-blog.csdnimg.cn/direct/a409a0f4555c459fa05c00fd9ee0ea8c.pngpicList>
      <collectCount>21collectCount>
    list>
    <list>
      <articleId>135244727articleId>
      <title>java lambda表达式训练题一title>
      <description>Lambda 表达式,也可称为闭包,它是推动 Java 8 发布的最重要新特性。Lambda 允许把函数作为一个方法的参数(函数作为参数传递进方法中)。使用 Lambda 表达式可以使代码变的更加简洁紧凑。description>
      <url>https://blog.csdn.net/qq_16127313/article/details/135244727url>
      <type>1type>
      <top>falsetop>
      <forcePlan>falseforcePlan>
      <viewCount>1238viewCount>
      <commentCount>0commentCount>
      <editUrl>https://editor.csdn.net/md?articleId=135244727editUrl>
      <postTime>2023-12-27 18:07:30postTime>
      <diggCount>7diggCount>
      <formatTime>2023.12.27formatTime>
      <picList>https://img-blog.csdnimg.cn/direct/d59c68b950754e879914b5319cd1b53f.pngpicList>
      <collectCount>8collectCount>
    list>
    <list>
      <articleId>135173565articleId>
      <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/135173565url>
      <type>1type>
      <top>falsetop>
      <forcePlan>falseforcePlan>
      <viewCount>1904viewCount>
      <commentCount>0commentCount>
      <editUrl>https://editor.csdn.net/md?articleId=135173565editUrl>
      <postTime>2023-12-23 20:17:11postTime>
      <diggCount>23diggCount>
      <formatTime>2023.12.23formatTime>
      <picList>https://img-blog.csdnimg.cn/direct/f0c994ca789a495a8c8c03d86d626f24.jpegpicList>
      <collectCount>23collectCount>
    list>
    <list>
      <articleId>135167613articleId>
      <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/135167613url>
      <type>1type>
      <top>falsetop>
      <forcePlan>falseforcePlan>
      <viewCount>1286viewCount>
      <commentCount>0commentCount>
      <editUrl>https://editor.csdn.net/md?articleId=135167613editUrl>
      <postTime>2023-12-23 13:52:23postTime>
      <diggCount>6diggCount>
      <formatTime>2023.12.23formatTime>
      <picList>https://img-blog.csdnimg.cn/direct/d3eeac85857543869dce8967c570bdc4.jpegpicList>
      <collectCount>11collectCount>
    list>
    <list>
      <articleId>135135799articleId>
      <title>【随笔】MD5加密字符串、文件apache、springframework实现title>
      <description>【代码】【随笔】MD5加密字符串、文件commons-codec、springframework实现。description>
      <url>https://blog.csdn.net/qq_16127313/article/details/135135799url>
      <type>1type>
      <top>falsetop>
      <forcePlan>falseforcePlan>
      <viewCount>1721viewCount>
      <commentCount>0commentCount>
      <editUrl>https://editor.csdn.net/md?articleId=135135799editUrl>
      <postTime>2023-12-21 17:29:54postTime>
      <diggCount>9diggCount>
      <formatTime>2023.12.21formatTime>
      <picList>https://img-blog.csdnimg.cn/direct/dc26b7f1c731494f80c8c3b3badfa95d.jpegpicList>
      <collectCount>9collectCount>
    list>
    <total>72total>
  data>
root>


三、代码实现

1. pom.xml


		<dependency>
			<groupId>org.springframeworkgroupId>
			<artifactId>spring-webfluxartifactId>
			<version>5.2.3.RELEASEversion>
		dependency>
		<dependency>
			<groupId>io.projectreactor.nettygroupId>
			<artifactId>reactor-nettyartifactId>
			<version>0.9.4.RELEASEversion>
		dependency>
		<dependency>
			<groupId>org.apache.logging.log4jgroupId>
			<artifactId>log4j-slf4j-implartifactId>
			<version>2.12.1version>
		dependency>
		<dependency>
			<groupId>org.apache.commonsgroupId>
			<artifactId>commons-lang3artifactId>
			<version>3.10version>
		dependency>
		<dependency>
			<groupId>commons-iogroupId>
			<artifactId>commons-ioartifactId>
			<version>2.5version>
		dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.dataformatgroupId>
			<artifactId>jackson-dataformat-xmlartifactId>
			<version>2.10.2version>
		dependency>
		<dependency>
		    <groupId>org.dom4jgroupId>
		    <artifactId>dom4jartifactId>
		    <version>2.1.4version>
		dependency>
		<dependency>
		    <groupId>jaxengroupId>
		    <artifactId>jaxenartifactId>
		    <version>2.0.0version>
		dependency>
		<dependency>
			<groupId>org.junit.jupitergroupId>
			<artifactId>junit-jupiter-apiartifactId>
			<version>5.5.2version>
			<scope>testscope>
		dependency>
		<dependency>
			<groupId>org.projectlombokgroupId>
			<artifactId>lombokartifactId>
			<version>1.18.12version>
			<scope>providedscope>
		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. 运行结果

java解析json复杂数据的第三种思路_第1张图片

四、源码传送

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


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

-over-

你可能感兴趣的:(雕虫小技,Java,java,json)