温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java中利用gson解析Json实例教程

发布时间:2020-08-28 23:24:05 来源:脚本之家 阅读:176 作者:jihite 栏目:编程语言

前言

本文主要跟大家介绍了关于Java用gson解析Json的相关内容,分享出来供大家参考学习,需要的朋友们下面来一起看看吧。

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

} 

解析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();
  }
 }
}

注意:文件路径相对路径是从工程根目录开始

Java中利用gson解析Json实例教程

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI