java解析json復(fù)雜數(shù)據(jù)的方法詳解
一、簡單分析
對照需求,可以簡單分解為如下步驟:
每天23點自動運行任務(wù),實現(xiàn)方式:@Scheduled、cron表達式
獲取每篇文章的訪問量,實現(xiàn)方式:api接口、數(shù)據(jù)解析
記錄每篇文章的訪問量,實現(xiàn)方式:javaBean、JDBC、ORM框架
二、具體實現(xiàn)一
步驟1、3代碼實現(xiàn)比較常見,不再詳細描述具體實現(xiàn)。
下面我們詳細描述步驟2的具體實現(xiàn)
1. api接口
接口地址: https://blog.csdn.net/community/home-api/v1/get-business-list?page={頁號}&size={當(dāng)前頁數(shù)據(jù)條數(shù)}&businessType=blog&username={用戶名}
2. 接口返回
以下面的接口為例:
https://blog.csdn.net/community/home-api/v1/get-business-list?page=1&size=5&businessType=blog&username=qq_16127313
返回數(shù)據(jù)如下:
{ "code": 200, "message": "success", "traceId": "5a64396a-caf3-49ad-a6db-022c55660b75", "data": { "list": [ { "articleId": 135244727, "title": "java lambda表達式訓(xùn)練題一", "description": "Lambda 表達式,也可稱為閉包,它是推動 Java 8 發(fā)布的最重要新特性。Lambda 允許把函數(shù)作為一個方法的參數(shù)(函數(shù)作為參數(shù)傳遞進方法中)。使用 Lambda 表達式可以使代碼變的更加簡潔緊湊。", "url": "https://blog.csdn.net/qq_16127313/article/details/135244727", "type": 1, "top": false, "forcePlan": false, "viewCount": 1019, "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 實現(xiàn)續(xù) - web api封裝", "description": "在 二維碼初體驗 com.google.zxing 實現(xiàn) 我們實現(xiàn)了二維碼的生成,但是大部分情況下,二維碼的相關(guān)功能是作為API接口來提供服務(wù)的。我們下面便演示在springboot、Knife4j下封裝api接口來實現(xiàn)二維碼生成功能。如何使用下面的備份文件恢復(fù)成原始的項目代碼,請移步查閱:神奇代碼恢復(fù)工具-over-", "url": "https://blog.csdn.net/qq_16127313/article/details/135173565", "type": 1, "top": false, "forcePlan": false, "viewCount": 1693, "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 實現(xiàn)", "description": "Java 操作二維碼的開源項目很多,如 SwetakeQRCode、BarCode4j、Zxing 等,這邊以Zxing 為例進行介紹。選擇需要生成QR原始文件,支持 “清除空白行及空格” 以減少二維碼圖片大小。支持輸入文本內(nèi)容,直接生成二維碼代碼結(jié)構(gòu)QrCodeUI: 完整版本代碼SimpleQrCodeUI:簡化版本代碼如何使用下面的備份文件恢復(fù)成原始的項目代碼,請移步查閱:神奇代碼恢復(fù)工具-over-", "url": "https://blog.csdn.net/qq_16127313/article/details/135167613", "type": 1, "top": false, "forcePlan": false, "viewCount": 1081, "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實現(xiàn)", "description": "【代碼】【隨筆】MD5加密字符串、文件commons-codec、springframework實現(xiàn)。", "url": "https://blog.csdn.net/qq_16127313/article/details/135135799", "type": 1, "top": false, "forcePlan": false, "viewCount": 1507, "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 }, { "articleId": 135087188, "title": "【隨筆】java工程中JSON 字符串格式化輸出", "description": "json字符串格式化輸出fastjson、gson、jackson實現(xiàn)。", "url": "https://blog.csdn.net/qq_16127313/article/details/135087188", "type": 1, "top": false, "forcePlan": false, "viewCount": 1198, "commentCount": 0, "editUrl": "https://editor.csdn.net/md?articleId=135087188", "postTime": "2023-12-19 17:07:40", "diggCount": 8, "formatTime": "2023.12.19", "picList": [ "https://img-blog.csdnimg.cn/direct/058249a1749e4ff5b62e1fcabf516c37.png" ], "collectCount": 6 } ], "total": 71 } }
3. json 數(shù)據(jù)解析
在Java中,可以使用多種庫來解析JSON數(shù)據(jù)。其中最常用的是fastjson、gson和jackson。
這邊我們以jackson為例來說明。
1.)引入Jackson庫
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.13.0</version> </dependency>
2.)定義實體
注意這邊實體屬性一定要跟json數(shù)據(jù)字段key對應(yīng),否則會解析報錯
@Data class BlogData { private Integer code; private String message; private String traceId; private Record data; } @Data class Record { private List<SubList> list; private Long total; } @Data class SubList { String articleId; String title; String description; String url; Integer type; String top; String forcePlan; Long viewCount; Long commentCount; String editUrl; String postTime; Long diggCount; String formatTime; Object picList; Long collectCount; }
3.)解析json字符串
ObjectMapper mapper = new ObjectMapper(); String url = "https://blog.csdn.net/community/home-api/v1/get-business-list?page=1&size=5&businessType=blog&username=qq_16127313"; String resp = webClient.get().uri(url).acceptCharset(StandardCharsets.UTF_8).accept(MediaType.APPLICATION_JSON).retrieve().bodyToMono(String.class).block(); BlogData blogData = mapper.readValue(resp, BlogData.class); log.info("blogData: {} ", blogData);
4.)運行結(jié)果
4. 過程分析
分析整個json數(shù)據(jù)解析過程,我們發(fā)現(xiàn)在實體定義步驟上,花費了我們太多精力和時間,而且一旦json數(shù)據(jù)key命名有變化或?qū)傩悦粚?yīng),便會打印類似下面的錯誤信息:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "collectCount" (class com.fly.test.restful.json.SubList), not marked as ignorable (15 known properties: "formatTime", "editUrl", "picList", "viewCount", "collectCount1", "postTime", "url", "commentCount", "articleId", "forcePlan", "top", "title", "type", "description", "diggCount"])
at [Source: (String)"{"code":200,"message":"success","traceId":"ce6fbb2f-72da-4a9c-9cf4-4e532a942210","data":{"list":[{"articleId":135244727,"title":"java lambda表達式訓(xùn)練題一","description":"Lambda 表達式,也可稱為閉包,它是推動 Java 8 發(fā)布的最重要新特性。Lambda 允許把函數(shù)作為一個方法的參數(shù)(函數(shù)作為參數(shù)傳遞進方法中)。使用 Lambda 表達式可以使代碼變的更加簡潔緊湊。","url":"https://blog.csdn.net/qq_16127313/article/details/135244727","type":1,"top":false,"forcePlan":false,"viewCount":1081,"commentCount":0,"editUrl":"https://editor.csdn.net/md?articleId=135244727","postTime":"2023-12-27 18:07:30"[truncated 2457 chars]; line: 1, column: 645] (through reference chain: com.fly.test.restful.json.BlogData["data"]->com.fly.test.restful.json.Record["list"]->java.util.ArrayList[0]->com.fly.test.restful.json.SubList["collectCount"])
at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:61)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:840)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1206)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1592)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1570)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:294)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:286)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:245)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:27)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:129)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:288)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:129)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:288)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4202)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3205)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3173)
at com.fly.test.restful.json.ParseJson.test2(ParseJson.java:57)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:675)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:125)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:132)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:124)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:74)
at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:104)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:62)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:43)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:35)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:202)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:198)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:135)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:69)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:135)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at java.util.ArrayList.forEach(ArrayList.java:1257)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at java.util.ArrayList.forEach(ArrayList.java:1257)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:229)
at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:197)
at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:211)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:191)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:137)
at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:98)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:40)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:529)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:756)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
三、具體實現(xiàn)二
在上面的實現(xiàn)中,通過分析得知,我們關(guān)注的信息只有url、viewCount這2個字段值,并不關(guān)心其他字段信息。
在java工程中JSON 字符串格式化輸出 中我們已經(jīng)實現(xiàn)了將json字符串格式化后輸出,仔細觀察輸出信息,會發(fā)現(xiàn)簡單數(shù)據(jù)對象(如key、value均為字符串、數(shù)字等類型數(shù)據(jù))均在同一行,這樣我們通過解析輸出的行字符串信息,便能得到對應(yīng)信息。
1. 核心代碼
// 調(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 resp = webClient.get().uri(url).acceptCharset(StandardCharsets.UTF_8).accept(MediaType.APPLICATION_JSON).retrieve().bodyToMono(String.class).block(); // 數(shù)據(jù)格式化-jackson格式化后會在:前后添加空格 String pretty = mapper.readTree(resp).toPrettyString(); List<String> lines = IOUtils.readLines(new StringReader(pretty)); // 獲取url List<String> urls = lines.stream().filter(line -> StringUtils.contains(line, "\"url\"")).map(n -> StringUtils.substringBetween(n, " : \"", "\",")).collect(Collectors.toList()); urls.stream().forEach(log::info); // 獲取viewCount List<String> viewCounts = lines.stream().filter(line -> StringUtils.contains(line, "\"viewCount\"")).map(n -> StringUtils.substringBetween(n, " : ", ",")).collect(Collectors.toList()); viewCounts.stream().forEach(log::info);
2.運行結(jié)果
四、方案比較
目標(biāo) | 方案一 | 方案二 |
---|---|---|
復(fù)雜度 | 復(fù)雜 | 簡單 |
健壯性 | 稍不足,需保證實體屬性與json key全部嚴(yán)格對應(yīng) | 健壯,只需保證獲取數(shù)據(jù)key正確 |
五、源碼傳送
到此這篇關(guān)于java解析json復(fù)雜數(shù)據(jù)的方法詳解的文章就介紹到這了,更多相關(guān)java解析json復(fù)雜數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot新建項目jdk只有17/21,無法選中1.8解決辦法
最近博主也有創(chuàng)建springboot項目,發(fā)現(xiàn)了IntelliJ IDEA在通過Spring Initilizer初始化項目的時候已經(jīng)沒有java8版本的選項了,這里給大家總結(jié)下,這篇文章主要給大家介紹了springboot新建項目jdk只有17/21,無法選中1.8的解決辦法,需要的朋友可以參考下2023-12-12java開發(fā)之基于Validator接口的SpringMVC數(shù)據(jù)校驗方式
這篇文章主要介紹了java開發(fā)之基于Validator接口的SpringMVC數(shù)據(jù)校驗方式,文中附含詳細示例代碼,有需要的朋友可以借鑒參考下2021-09-09MyBatis-Plus中MetaObjectHandler沒生效完美解決
在進行測試時發(fā)現(xiàn)配置的MyMetaObjectHandler并沒有生效,本文主要介紹了MyBatis-Plus中MetaObjectHandler沒生效完美解決,具有一定的參考價值,感興趣的可以了解一下2023-11-11