Java中利用gson解析Json实例教程
程序员文章站
2024-02-13 23:26:16
前言
本文主要跟大家介绍了关于java用gson解析json的相关内容,分享出来供大家参考学习,需要的朋友们下面来一起看看吧。
json数据
{
"r...
前言
本文主要跟大家介绍了关于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中JSON解析器的一些见解
-
如何利用JAVA正则表达式轻松替换JSON中的大字段
-
Java高并发中的伪共享,getUnsafe源码解析并利用反射获取Unsafe实例
-
java中利用dom4j解析XML文件
-
Ruby和Ruby on Rails中解析JSON格式数据的实例教程
-
Android 中Json解析的几种框架(Gson、Jackson、FastJson、LoganSquare)使用与对比
-
【Java】利用json工具类,传入字段名,获取集合中的对象属性值集合
-
如何利用JAVA正则表达式轻松替换JSON中的大字段