Java中利用gson解析Json實(shí)例教程
前言
本文主要跟大家介紹了關(guān)于Java用gson解析Json的相關(guān)內(nèi)容,分享出來(lái)供大家參考學(xué)習(xí),需要的朋友們下面來(lái)一起看看吧。
json數(shù)據(jù)
{
"resultcode": "200",
"reason": "successed!",
"result": {
"sk": {
"temp": "24",
"wind_direction": "西南風(fēng)",
"wind_strength": "2級(jí)",
"humidity": "51%",
"time": "10:11"
},
"today": {
"temperature": "16℃~27℃",
"weather": "陰轉(zhuǎn)多云",
"weather_id": {
"fa": "02",
"fb": "01"
},
"wind": "西南風(fēng)3-4 級(jí)",
"week": "星期四",
"city": "濱州",
"date_y": "2015年06月04日",
"dressing_index": "舒適",
"dressing_advice": "建議著長(zhǎng)袖T恤、襯衫加單褲等服裝。年老體弱者宜著針織長(zhǎng)袖襯衫、馬甲和長(zhǎng)褲。",
"uv_index": "最弱",
"comfort_index": "",
"wash_index": "較適宜",
"travel_index": "",
"exercise_index": "較適宜",
"drying_index": ""
},
"future": [
{
"temperature": "16℃~27℃",
"weather": "陰轉(zhuǎn)多云",
"weather_id": {
"fa": "02",
"fb": "01"
},
"wind": "西南風(fēng)3-4 級(jí)",
"week": "星期四",
"date": "20150604"
},
{
"temperature": "20℃~32℃",
"weather": "多云轉(zhuǎn)晴",
"weather_id": {
"fa": "01",
"fb": "00"
},
"wind": "西風(fēng)3-4 級(jí)",
"week": "星期五",
"date": "20150605"
},
{
"temperature": "23℃~35℃",
"weather": "多云轉(zhuǎn)陰",
"weather_id": {
"fa": "01",
"fb": "02"
},
"wind": "西南風(fēng)3-4 級(jí)",
"week": "星期六",
"date": "20150606"
},
{
"temperature": "20℃~33℃",
"weather": "多云",
"weather_id": {
"fa": "01",
"fb": "01"
},
"wind": "北風(fēng)微風(fēng)",
"week": "星期日",
"date": "20150607"
},
{
"temperature": "22℃~34℃",
"weather": "多云",
"weather_id": {
"fa": "01",
"fb": "01"
},
"wind": "西南風(fēng)3-4 級(jí)",
"week": "星期一",
"date": "20150608"
},
{
"temperature": "22℃~33℃",
"weather": "陰",
"weather_id": {
"fa": "02",
"fb": "02"
},
"wind": "西南風(fēng)3-4 級(jí)",
"week": "星期二",
"date": "20150609"
},
{
"temperature": "22℃~33℃",
"weather": "多云",
"weather_id": {
"fa": "01",
"fb": "01"
},
"wind": "南風(fēng)3-4 級(jí)",
"week": "星期三",
"date": "20150610"
}
]
},
"error_code": 0
}
解析JSONObject
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import com.google.gson.JsonIOException;
import java.io.FileNotFoundException;
import java.io.FileReader;
public class ReadJson {
public static void main(String []args) {
JsonParser parse = new JsonParser();
try {
JsonObject json = (JsonObject) parse.parse(new FileReader("weather.json"));
System.out.println("resultcode:" + json.get("resultcodeu").getAsInt());
System.out.println("reason:" + json.get("reason").getAsString());
JsonObject result = json.get("result").getAsJsonObject();
JsonObject today = result.get("today").getAsJsonObject();
System.out.println("weak:" + today.get("week").getAsString());
System.out.println("weather:" + today.get("weather").getAsString());
} catch (JsonIOException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
} catch (JsonSyntaxException e){
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
解析JSONArray
import com.google.gson.JsonParser;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import com.google.gson.JsonIOException;
import java.io.FileNotFoundException;
import java.io.FileReader;
public class ReadJsonArray {
public static void main(String []args) {
JsonParser parse = new JsonParser();
try {
JsonObject json = (JsonObject)parse.parse(new FileReader("C:\\Users\\wzh94434\\IdeaProjects\\TestProject\\jsontest\\src\\main\\java\\weather.json"));
JsonObject result = json.get("result").getAsJsonObject();
JsonArray futureArray = result.get("future").getAsJsonArray();
for (int i = 0; i < futureArray.size(); ++i) {
JsonObject subObj = futureArray.get(i).getAsJsonObject();
System.out.println("------");
System.out.println("week:" + subObj.get("week").getAsString());
System.out.println("weather:" + subObj.get("weather").getAsString());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (JsonIOException e) {
e.printStackTrace();
} catch (JsonSyntaxException e) {
e.printStackTrace();
}
}
}
注意:文件路徑相對(duì)路徑是從工程根目錄開(kāi)始

總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Spring boot監(jiān)控Actuator-Admin實(shí)現(xiàn)過(guò)程詳解
這篇文章主要介紹了Spring boot監(jiān)控Actuator-Admin實(shí)現(xiàn)過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
Java語(yǔ)言多線程終止中的守護(hù)線程實(shí)例
這篇文章主要介紹了Java語(yǔ)言多線程終止中的守護(hù)線程實(shí)例,具有一定借鑒價(jià)值,需要的朋友可以參考下2017-12-12
Java 實(shí)戰(zhàn)項(xiàng)目錘煉之校園宿舍管理系統(tǒng)的實(shí)現(xiàn)流程
讀萬(wàn)卷書(shū)不如行萬(wàn)里路,只學(xué)書(shū)上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+jsp+javaweb+mysql+ajax實(shí)現(xiàn)一個(gè)校園宿舍管理系統(tǒng),大家可以在過(guò)程中查缺補(bǔ)漏,提升水平2021-11-11
SpringMVC使用@PathVariable接收參數(shù)過(guò)程解析
這篇文章主要介紹了SpringMVC使用@PathVariable接收參數(shù)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
Java getRealPath("/")與getContextPath()區(qū)別詳細(xì)分析
這篇文章主要介紹了Java getRealPath("/")與getContextPath()區(qū)別詳細(xì)分析,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
解決打開(kāi)的idea項(xiàng)目maven不生效問(wèn)題
這篇文章主要給大家介紹了關(guān)于如何解決打開(kāi)的idea項(xiàng)目maven不生效問(wèn)題,最近在配置maven時(shí),發(fā)現(xiàn)無(wú)論配置幾遍,IDEA中的maven配置總會(huì)還原成默認(rèn)的,所以這里給大家分享下解決辦法,需要的朋友可以參考下2023-07-07

