Jackson將json string轉(zhuǎn)為Object,org.json讀取json數(shù)組的實(shí)例
從json文件讀取json string或者自定義json string,將其轉(zhuǎn)為object。下面采用的object為map,根據(jù)map讀取json的某個(gè)數(shù)據(jù),可以讀取第一級(jí)的數(shù)據(jù)name,后來發(fā)現(xiàn)想轉(zhuǎn)成JsonArray讀取”red“時(shí)沒撤了,只好用了其他方法。
最后用org.json包解決了(readJsonArray函數(shù)),有空再看看有沒有更好的辦法。
JSON文件如下:
{ "name":"name", "id":"id", "color":[ {"red":"red","blue":"blue"}, {"white":"white"} ] }
代碼如下:
package com; import org.codehaus.jackson.map.ObjectMapper; import org.json.JSONArray; import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.*; import java.util.Map; /** * Hello world! * */ public class JsonAnalysis { private static final Logger LOG = LoggerFactory.getLogger(JsonAnalysis.class); public static void main(String[] args) throws FileNotFoundException { String jsonString = "{\"address\":\"address\",\"name\":\"name\",\"id\":\"1\",\"email\":\"email\"}"; FileReader fileReader = new FileReader("E:\\JsonAnalysis\\src\\test.json"); String fileString = readFile(fileReader); //Json字符串轉(zhuǎn)java對(duì)象,比如轉(zhuǎn)為Map對(duì)象讀取其中數(shù)據(jù) Map map = null; Map mapFile = null; try { map = readValue(jsonString, Map.class); mapFile = readValue(fileString, Map.class); } catch (Exception e) { e.printStackTrace(); LOG.error("ReadValue occur exception when switch json string to map"); } System.out.println(map != null ? map.get("id") : null); if (mapFile==null){ LOG.info("Json map form file is empty"); return; } System.out.println(mapFile.get("name")); try { readJsonArray(fileString); } catch (Exception e) { e.printStackTrace(); } } //Json string to object private static <T> T readValue(String jsonStr, Class<T> valueType) throws Exception{ ObjectMapper objectMapper = new ObjectMapper(); try { // Object object = objectMapper.readValue(jsonStr,Object.class); return objectMapper.readValue(jsonStr,valueType); } catch (IOException e) { e.printStackTrace(); } return null; } //Read file and to string private static String readFile(FileReader fileReader){ BufferedReader bufferedReader = new BufferedReader(fileReader); StringBuilder fileStr = new StringBuilder(); try { String eachLine; while ((eachLine=bufferedReader.readLine())!=null){ fileStr.append(eachLine); } return fileStr.toString(); } catch (IOException e1) { e1.printStackTrace(); LOG.error("Occur exception when read file,file={}",fileReader); return null; } } //根據(jù)json string 獲取json array,讀取數(shù)據(jù)( 注意該部分引用的是org.json 包) private static void readJsonArray(String jsonStr) throws Exception { JSONObject jsonObject = new JSONObject(jsonStr); JSONArray jsonArray = jsonObject.getJSONArray("color"); JSONObject jsonObject1 = jsonArray.getJSONObject(0); System.out.println(jsonObject1.get("red")); } }
以上這篇Jackson將json string轉(zhuǎn)為Object,org.json讀取json數(shù)組的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- JS數(shù)組Object.keys()方法的使用示例
- JS實(shí)現(xiàn)的Object數(shù)組去重功能示例【數(shù)組成員為Object對(duì)象】
- 詳談js中數(shù)組(array)和對(duì)象(object)的區(qū)別
- JavaScript從數(shù)組的indexOf()深入之Object的Property機(jī)制
- JavaScript中使用Object.prototype.toString判斷是否為數(shù)組
- javascript 對(duì)象數(shù)組根據(jù)對(duì)象object key的值排序
- Javascript中判斷變量是數(shù)組還是對(duì)象(array還是object)
- JS中比較兩個(gè)Object數(shù)組是否相等方法實(shí)例
相關(guān)文章
springmvc @ResponseStatus和ResponseEntity的使用
這篇文章主要介紹了springmvc @ResponseStatus和ResponseEntity的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07HttpClient 在Java項(xiàng)目中的使用詳解
HttpClient作為訪問Http服務(wù)的客戶端訪問程序已經(jīng)被廣泛使用,提高了開發(fā)效率,也提高了代碼的健壯性。因此熟練掌握HttpClient是必需的,關(guān)于httpclient感興趣的朋友可以參考本篇文章2015-10-10java中的類型自動(dòng)轉(zhuǎn)換機(jī)制解析
這篇文章主要介紹了java中的類型自動(dòng)轉(zhuǎn)換機(jī)制,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03java實(shí)現(xiàn)interceptor攔截登錄權(quán)限
Java里的攔截器是動(dòng)態(tài)攔截action調(diào)用的對(duì)象,本文主要介紹了java實(shí)現(xiàn)interceptor攔截登錄權(quán)限,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09SpringBoot中@ConditionalOnBean實(shí)現(xiàn)原理解讀
這篇文章主要介紹了SpringBoot中@ConditionalOnBean實(shí)現(xiàn)原理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02Spring Task定時(shí)任務(wù)的配置和使用詳解
本篇文章主要介紹了Spring Task定時(shí)任務(wù)的配置和使用詳解,實(shí)例分析了Spring Task定時(shí)任務(wù)的配置和使用的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-04-04在CentOS7(有圖形化界面)上安裝maven和idea的詳細(xì)教程
這篇文章主要介紹了在CentOS7(有圖形化界面)上安裝maven和idea的詳細(xì)教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03