日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

java解析json_JAVA解析JSON数据

發布時間:2023/12/4 javascript 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java解析json_JAVA解析JSON数据 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在使用第三方api的使用,有時候會從網絡中獲得json數據,所以說我們將如何解析json數據?

下面小編將通過以下幾點來進行json的講解

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of theJavaScript Programming Language,Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.

2.Json數據類型

2-1.json對象

2-2.json數組

ps:JSONObject與JSONArray的區別

(JSON數組)

(JSON數組)

3.解析JSON數據(小編使用的GSON進行json數據的解析)

3-1 【JSONObject的解析】

下面是一個json文件:

{

"resultcode": "200",

"reason": "successed!",

"result": {

"sk": {

"temp": "24",

"wind_direction": "西南風",

"wind_strength": "2級",

"humidity": "51%",

"time": "10:11"

},

"today": {

"temperature": "16℃~27℃",

"weather": "陰轉多云",

"weather_id": {

"fa": "02",

"fb": "01"

},

"wind": "西南風3-4 級",

"week": "星期四",

"city": "濱州",

"date_y": "2015年06月04日",

"dressing_index": "舒適",

"dressing_advice": "建議著長袖T恤、襯衫加單褲等服裝。年老體弱者宜著針織長袖襯衫、馬甲和長褲。",

"uv_index": "最弱",

"comfort_index": "",

"wash_index": "較適宜",

"travel_index": "",

"exercise_index": "較適宜",

"drying_index": ""

},

"future": [

{

"temperature": "16℃~27℃",

"weather": "陰轉多云",

"weather_id": {

"fa": "02",

"fb": "01"

},

"wind": "西南風3-4 級",

"week": "星期四",

"date": "20150604"

},

{

"temperature": "20℃~32℃",

"weather": "多云轉晴",

"weather_id": {

"fa": "01",

"fb": "00"

},

"wind": "西風3-4 級",

"week": "星期五",

"date": "20150605"

},

{

"temperature": "23℃~35℃",

"weather": "多云轉陰",

"weather_id": {

"fa": "01",

"fb": "02"

},

"wind": "西南風3-4 級",

"week": "星期六",

"date": "20150606"

},

{

"temperature": "20℃~33℃",

"weather": "多云",

"weather_id": {

"fa": "01",

"fb": "01"

},

"wind": "北風微風",

"week": "星期日",

"date": "20150607"

},

{

"temperature": "22℃~34℃",

"weather": "多云",

"weather_id": {

"fa": "01",

"fb": "01"

},

"wind": "西南風3-4 級",

"week": "星期一",

"date": "20150608"

},

{

"temperature": "22℃~33℃",

"weather": "陰",

"weather_id": {

"fa": "02",

"fb": "02"

},

"wind": "西南風3-4 級",

"week": "星期二",

"date": "20150609"

},

{

"temperature": "22℃~33℃",

"weather": "多云",

"weather_id": {

"fa": "01",

"fb": "01"

},

"wind": "南風3-4 級",

"week": "星期三",

"date": "20150610"

}

]

},

"error_code": 0

}

我們進行解析(解析一部分):

package cn.edu.bzu.json;

import java.io.FileNotFoundException;

import java.io.FileReader;

import com.google.gson.JsonArray;

import com.google.gson.JsonIOException;

import com.google.gson.JsonObject;

import com.google.gson.JsonParser;

import com.google.gson.JsonSyntaxException;

public class Read {

public static void main(String args[]){

JsonParser parse =new JsonParser(); //創建json解析器

try {

JsonObject json=(JsonObject) parse.parse(new FileReader("weather.json")); //創建jsonObject對象

System.out.println("resultcode:"+json.get("resultcode").getAsInt()); //將json數據轉為為int型的數據

System.out.println("reason:"+json.get("reason").getAsString()); //將json數據轉為為String型的數據

JsonObject result=json.get("result").getAsJsonObject();

JsonObject today=result.get("today").getAsJsonObject();

System.out.println("temperature:"+today.get("temperature").getAsString());

System.out.println("weather:"+today.get("weather").getAsString());

} catch (JsonIOException e) {

e.printStackTrace();

} catch (JsonSyntaxException e) {

e.printStackTrace();

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

}

輸出結果:

3-2 【JSONArray的解析】

下面是一個json文件

{"cat":"it","language":[

{"id":1,"ide":"eclipse","name":Java},

{"id":2,"ide":"XCode","name":"Swift"},

{"id":3,"ide":"Visual Stdio","name":"C#"}

],"pop":true}

我們進行解析:

package cn.edu.bzu.json;

import java.io.FileNotFoundException;

import java.io.FileReader;

import com.google.gson.JsonArray;

import com.google.gson.JsonIOException;

import com.google.gson.JsonObject;

import com.google.gson.JsonParser;

import com.google.gson.JsonSyntaxException;

public class ReadJSON {

public static void main(String args[]){

try {

JsonParser parser=new JsonParser(); //創建JSON解析器

JsonObject object=(JsonObject) parser.parse(new FileReader("test.json")); //創建JsonObject對象

System.out.println("cat="+object.get("cat").getAsString()); //將json數據轉為為String型的數據

System.out.println("pop="+object.get("pop").getAsBoolean()); //將json數據轉為為boolean型的數據

JsonArray array=object.get("language").getAsJsonArray(); //得到為json的數組

for(int i=0;i

System.out.println("---------------");

JsonObject subObject=array.get(i).getAsJsonObject();

System.out.println("id="+subObject.get("id").getAsInt());

System.out.println("name="+subObject.get("name").getAsString());

System.out.println("ide="+subObject.get("ide").getAsString());

}

} catch (JsonIOException e) {

e.printStackTrace();

} catch (JsonSyntaxException e) {

e.printStackTrace();

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

}

輸出結果:

3-3 【分析】

我們通過Gson進行解析,所以在使用前需要導入Gson.jar

解析json數據時,

1.需要進行創建Gson解析器

2.創建JSONObject對象

3.將json數據轉為為相應的數據

4.源代碼下載:

總結

以上是生活随笔為你收集整理的java解析json_JAVA解析JSON数据的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。