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

发布时间 - 2026-01-11 01:15:56    点击率:

前言

本文主要跟大家介绍了关于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  # java如何解析json  # JAVA使用Gson解析json数据实例解析  # 基于JAVA中的四种JSON解析方式详解  # 如何使用GSON解析JSON数据  # 滨州  # 相关内容  # 是从  # 朋友们  # 这篇文章  # 谢谢大家  # 看看吧  # 最弱  # 星期日  # 转阴  # 转晴  # 体弱者  # 有疑问  # city  # week  # date_y  # fa  # fb  # weather_id  # wind 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: 安克发布新款氮化镓充电宝:体积缩小 30%,支持 200W 输出  Laravel怎么清理缓存_Laravel optimize clear命令详解  如何快速搭建高效可靠的建站解决方案?  iOS验证手机号的正则表达式  Laravel如何优化应用性能?(缓存和优化命令)  ChatGPT常用指令模板大全 新手快速上手的万能Prompt合集  昵图网官网入口 昵图网素材平台官方入口  HTML5空格和margin有啥区别_空格与外边距的使用场景【说明】  b2c电商网站制作流程,b2c水平综合的电商平台?  微信小程序 scroll-view组件实现列表页实例代码  在线教育网站制作平台,山西立德教育官网?  Java类加载基本过程详细介绍  Laravel项目结构怎么组织_大型Laravel应用的最佳目录结构实践  如何用PHP快速搭建高效网站?分步指南  装修招标网站设计制作流程,装修招标流程?  如何挑选最适合建站的高性能VPS主机?  Python面向对象测试方法_mock解析【教程】  奇安信“盘古石”团队突破 iOS 26.1 提权  智能起名网站制作软件有哪些,制作logo的软件?  QQ浏览器网页版登录入口 个人中心在线进入  济南网站建设制作公司,室内设计网站一般都有哪些功能?  logo在线制作免费网站在线制作好吗,DW网页制作时,如何在网页标题前加上logo?  Windows10如何更改计算机工作组_Win10系统属性修改Workgroup  Laravel如何使用Blade模板引擎?(完整语法和示例)  Java遍历集合的三种方式  原生JS实现图片轮播切换效果  Laravel如何实现事件和监听器?(Event & Listener实战)  Linux系统命令中screen命令详解  Bootstrap整体框架之JavaScript插件架构  浅谈redis在项目中的应用  如何确保FTP站点访问权限与数据传输安全?  Laravel怎么做数据加密_Laravel内置Crypt门面的加密与解密功能  rsync同步时出现rsync: failed to set times on “xxxx”: Operation not permitted  Laravel如何使用Eloquent进行子查询  java中使用zxing批量生成二维码立牌  Laravel如何实现数据导出到PDF_Laravel使用snappy生成网页快照PDF【方案】  Python高阶函数应用_函数作为参数说明【指导】  Windows10怎样连接蓝牙设备_Windows10蓝牙连接步骤【教程】  免费的流程图制作网站有哪些,2025年教师初级职称申报网上流程?  如何在VPS电脑上快速搭建网站?  Edge浏览器提示“由你的组织管理”怎么解决_去除浏览器托管提示【修复】  如何在自有机房高效搭建专业网站?  如何快速登录WAP自助建站平台?  Laravel怎么使用Blade模板引擎_Laravel模板继承与Component组件复用【手册】  武汉网站设计制作公司,武汉有哪些比较大的同城网站或论坛,就是里面都是武汉人的?  Laravel怎么多语言本地化设置_Laravel语言包翻译与Locale动态切换【手册】  如何在宝塔面板创建新站点?  js实现点击每个li节点,都弹出其文本值及修改  如何在腾讯云服务器上快速搭建个人网站?  Laravel如何实现多对多模型关联?(Eloquent教程)