java讀取JSON文件的多種實(shí)現(xiàn)方式
在開發(fā)過程中有時(shí)會(huì)遇到需要讀取本地.json文件的需求,通常會(huì)自己寫Reader代碼去讀,但是這么做寫出來(lái)的代碼有些繁瑣(需要關(guān)流、創(chuàng)建StringBuilder對(duì)象等操作)。
最近發(fā)現(xiàn)幾個(gè)小工具可以讓需求代碼變得更加簡(jiǎn)潔。
準(zhǔn)備
json文件:F:\halon.json
{ "ID": 10001, "detail": "detail", "json_format_version": 1.0, "other_info": { "array_one": [ [855, 410], [854, 411], [847, 411], [846, 410], [845, 410], [844, 409] ], "array_two": [ [832, 303], [829, 303], [828, 302], [825, 302], [824, 301] ], "array_three": [ [1013, 224], [1012, 225], [1010, 225], [1009, 226], [1023, 224] ], "point": [853, 310], "boolean": true } }
1.使用FileReader讀取json文件
package com.tool; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.*; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Map; /** * @Author halon * @create 2021/9/ */ public class ReadLocalJsonFileDemo { public static void main(String[] args) throws IOException { File file = new File("F:\\halon.json"); readerMethod(file); } private static void readerMethod(File file) throws IOException { FileReader fileReader = new FileReader(file); Reader reader = new InputStreamReader(new FileInputStream(file), "Utf-8"); int ch = 0; StringBuffer sb = new StringBuffer(); while ((ch = reader.read()) != -1) { sb.append((char) ch); } fileReader.close(); reader.close(); String jsonStr = sb.toString(); System.out.println(JSON.parseObject(jsonStr)); } }
控制臺(tái)輸出:
{"other_info":{"array_two":[[832,303],[829,303],[828,302],[825,302],[824,301]],"array_three":[[1013,224],[1012,225],[1010,225],[1009,226],[1023,224]],"boolean":true,"array_one":[[855,410],[854,411],[847,411],[846,410],[845,410],[844,409]],"point":[853,310]},"ID":10001,"detail":"detail","json_format_version":1.0}
2.使用jacksonAPI讀取json文件
package com.tool; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.*; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Map; /** * @Author halon * @create 2021/9/ */ public class ReadLocalJsonFileDemo { public static void main(String[] args) throws IOException { File file = new File("F:\\halon.json"); jacksonMethod(file); } private static void jacksonMethod(File file) throws IOException { ObjectMapper objectMapper = new ObjectMapper(); System.out.println(objectMapper.readValue(file, Map.class)); }
控制臺(tái)輸出:
{ID=10001, detail=detail, json_format_version=1.0, other_info={array_one=[[855, 410], [854, 411], [847, 411], [846, 410], [845, 410], [844, 409]], array_two=[[832, 303], [829, 303], [828, 302], [825, 302], [824, 301]], array_three=[[1013, 224], [1012, 225], [1010, 225], [1009, 226], [1023, 224]], point=[853, 310], boolean=true}}
3.使用nio讀取json文件
package com.tool; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.*; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Map; /** * @Author halon * @create 2021/9/ */ public class ReadLocalJsonFileDemo { public static void main(String[] args) throws IOException { File file = new File("F:\\halon.json"); nioMethod(file); } private static void nioMethod(File file) throws IOException { String jsonString = new String(Files.readAllBytes(Paths.get(file.getPath()))); System.out.println(JSONObject.parseObject(jsonString)); }
控制臺(tái)輸出:
{"other_info":{"array_two":[[832,303],[829,303],[828,302],[825,302],[824,301]],"array_three":[[1013,224],[1012,225],[1010,225],[1009,226],[1023,224]],"boolean":true,"array_one":[[855,410],[854,411],[847,411],[846,410],[845,410],[844,409]],"point":[853,310]},"ID":10001,"detail":"detail","json_format_version":1.0}
未完待續(xù)。。。。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java如何實(shí)現(xiàn)海量數(shù)據(jù)判重
在海量數(shù)據(jù)如何確定一個(gè)值是否存在?這是一道非常經(jīng)典的面試場(chǎng)景題,那怎么回答這個(gè)問題呢?下面小編就來(lái)和大家詳細(xì)的聊一聊,感興趣的可以一起學(xué)習(xí)一下2023-09-09IDEA2022搭建Spring?Cloud多模塊項(xiàng)目的詳細(xì)過程
這篇文章主要介紹了IDEA2022搭建Spring?Cloud多模塊項(xiàng)目,網(wǎng)上有很多教程父模塊都是通過maven的方式創(chuàng)建的,然后子模塊是通過Spring?Initalizr方式創(chuàng)建,這種方式父模塊無(wú)法管理子模塊的依賴仲裁,需要每個(gè)子模塊自行管理,就失去了父模塊的用處了2022-10-10java 實(shí)現(xiàn)當(dāng)前時(shí)間加減30分鐘的時(shí)間代碼
這篇文章主要介紹了java 實(shí)現(xiàn)當(dāng)前時(shí)間加減30分鐘的時(shí)間代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-08-08如何使用Spring Batch進(jìn)行批處理任務(wù)管理
本文介紹了如何配置Spring Batch、如何創(chuàng)建批處理任務(wù),以及如何讀取和寫入數(shù)據(jù),希望通過本文的介紹,你能更好地理解和使用Spring Batch來(lái)管理批處理任務(wù),感興趣的朋友跟隨小編一起看看吧2024-08-08SSH框架網(wǎng)上商城項(xiàng)目第7戰(zhàn)之整合Struts2和Json
SSH框架網(wǎng)上商城項(xiàng)目第7戰(zhàn)之整合Struts2和Json,打通EasyUI和Struts2之間的交互,感興趣的小伙伴們可以參考一下2016-05-05spring boot整合log4j2及MQ消費(fèi)處理系統(tǒng)日志示例
這篇文章主要為大家介紹了spring boot整合log4j2及MQ消費(fèi)處理系統(tǒng)日志的示例過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03解決javaBean規(guī)范導(dǎo)致json傳參首字母大寫將永遠(yuǎn)獲取不到問題
這篇文章主要介紹了解決javaBean規(guī)范導(dǎo)致json傳參首字母大寫將永遠(yuǎn)獲取不到問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07