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

 更新时间:2017年05月24日 09:59:18   作者:jihite  
这篇文章主要给大家介绍了关于Java中利用gson解析Json 的相关资料,文中给出了详细的示例代码供大家参考学习,相信对大家具有一定的参考学习价值,需要的朋友们下面来一起看看吧。

前言

本文主要跟大家介绍了关于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();
  }
 }
}

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

总结

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

相关文章

  • velocity显示List与Map的方法详细解析

    velocity显示List与Map的方法详细解析

    以下是对velocity显示List与Map的方法进行了详细的介绍。需要的朋友可以过来参考下
    2013-08-08
  • 解读@Bean和@Autowired、@Resource之间的区别

    解读@Bean和@Autowired、@Resource之间的区别

    这篇文章主要介绍了@Bean和@Autowired、@Resource之间的区别及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2025-03-03
  • Java生成pdf文件或jpg图片的案例讲解

    Java生成pdf文件或jpg图片的案例讲解

    这篇文章主要介绍了Java生成pdf文件或jpg图片的案例讲解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-01-01
  • java回溯算法解数独问题

    java回溯算法解数独问题

    这篇文章主要为大家详细介绍了java回溯算法解数独问题,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-01-01
  • mybatis-plus数据权限实现代码

    mybatis-plus数据权限实现代码

    这篇文章主要介绍了mybatis-plus数据权限实现,结合了mybatis-plus的插件方式,做出了自己的注解方式的数据权限,虽然可能存在一部分的局限性,但很好的解决了我们自己去解析SQL的功能,需要的朋友可以参考下
    2023-06-06
  • 分布式事务CAP两阶段提交及三阶段提交详解

    分布式事务CAP两阶段提交及三阶段提交详解

    这篇文章主要为大家介绍了分布式事务CAP、两阶段提交及三阶段提交的内容详解有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-01-01
  • javaweb 项目初始配置的方法步骤

    javaweb 项目初始配置的方法步骤

    本文主要介绍了javaweb 项目初始配置的方法步骤,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-11-11
  • Springboot集成JAXB返回xml格式

    Springboot集成JAXB返回xml格式

    这篇文章主要为大家详细介绍了Springboot如何集成JAXB返回xml格式,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-12-12
  • MybatisPlus中如何调用Oracle存储过程

    MybatisPlus中如何调用Oracle存储过程

    这篇文章主要介绍了MybatisPlus中如何调用Oracle存储过程的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-05-05
  • Java 实现LZ78压缩算法的示例代码

    Java 实现LZ78压缩算法的示例代码

    这篇文章主要介绍了Java 实现LZ78压缩算法的示例代码,代码简单易懂,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-05-05

最新评论