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,后來(lái)發(fā)現(xiàn)想轉(zhuǎn)成JsonArray讀取”red“時(shí)沒(méi)撤了,只好用了其他方法。
最后用org.json包解決了(readJsonArray函數(shù)),有空再看看有沒(méi)有更好的辦法。
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)文章
SpringBoot用JdbcTemplates訪問(wèn)Mysql實(shí)例代碼
本篇文章主要介紹了SpringBoot用JdbcTemplates訪問(wèn)Mysql實(shí)例代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-05-05
Java中字符串常見(jiàn)題之String相關(guān)講解
今天小編就為大家分享一篇關(guān)于Java中字符串常見(jiàn)題之String相關(guān)講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01
大廠禁止SpringBoot在項(xiàng)目使用Tomcat容器原理解析
這篇文章主要為大家介紹了大廠禁止SpringBoot在項(xiàng)目使用Tomcat原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
SpringBoot2如何集成Elasticsearch6.x(TransportClient方式)
這篇文章主要介紹了SpringBoot2如何集成Elasticsearch6.x(TransportClient方式)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
Java?精煉解讀時(shí)間復(fù)雜度與空間復(fù)雜度
對(duì)于一個(gè)算法,其時(shí)間復(fù)雜度和空間復(fù)雜度往往是相互影響的,當(dāng)追求一個(gè)較好的時(shí)間復(fù)雜度時(shí),可能會(huì)使空間復(fù)雜度的性能變差,即可能導(dǎo)致占用較多的存儲(chǔ)空間,這篇文章主要給大家介紹了關(guān)于Java時(shí)間復(fù)雜度、空間復(fù)雜度的相關(guān)資料,需要的朋友可以參考下2022-03-03
使用Maven進(jìn)行多模塊項(xiàng)目管理的操作步驟
使用Maven進(jìn)行多模塊項(xiàng)目管理可以清晰組織大型項(xiàng)目結(jié)構(gòu),便于維護(hù)和構(gòu)建,詳細(xì)步驟包括創(chuàng)建父項(xiàng)目,設(shè)置pom.xml,創(chuàng)建子模塊,并配置子模塊的pom.xml,在父項(xiàng)目中管理依賴,通過(guò)命令行構(gòu)建項(xiàng)目,確保配置一致性2024-10-10

