本文主要跟大家介绍了关于Java用gson解析Json的相关内容,分享出来供大家参考学习,需要的朋友们下面来一起看看吧。
{ “resultcode”:“200”, “原因”:“成功了!” "结果":{ “sk”: { “临时”:“24”, “wind_direction”:“西南风”, “wind_strength”:“两级”, “湿度”:“51%”, “时间”:“11” }, "今天":{ “温度”:“16℃~ 27℃ “天气”:“阴转多云”, " weather_id ": { “fa”:“02”, “facebook”:“01” }, “风”:“西南风3 - 4级”, “周”:“星期四”, “城市”:“滨州”, “date_y”:“2015年06月04日”, “dressing_index”:“舒适”, “dressing_advice”:“建议着长袖T恤,衬衫加单裤等服装。年老体弱者宜着针织长袖衬衫,马甲和长裤。”, “uv_index”:“最弱”, “comfort_index”:“”, :“wash_index较适宜”, “travel_index”:“”, :“exercise_index较适宜”, “drying_index”:“ }, “未来”:( { “温度”:“16℃~ 27℃ “天气”:“阴转多云”, " weather_id ": { “fa”:“02”, “facebook”:“01” }, “风”:“西南风3 - 4级”, “周”:“星期四”, “日期”:“20150604” }, { “温度”:“20℃~ 32℃ “天气”:“多云转晴”, " weather_id ": { “fa”:“1”, “facebook”:“00” }, “风”:“西风3 - 4级”, “周”:“星期五”, “日期”:“20150605” }, { “温度”:“23℃~ 35℃”, “天气”:“多云转阴”, " weather_id ": { “fa”:“1”, “facebook”:“02” }, “风”:“西南风3 - 4级”, “周”:“星期六”, “日期”:“20150606” }, { :“温度20℃~ 33℃”, “天气”:“多云”, " weather_id ": { “fa”:“1”, “facebook”:“01” }, “风”:“北风微风”, “周”:“星期日”, “日期”:“20150607” }, { :“温度22℃~ 34℃”, “天气”:“多云”, " weather_id ": { “fa”:“1”, “facebook”:“01” }, “风”:“西南风3 - 4级”, “周”:“星期一”, “日期”:“20150608” }, { “温度”:“22℃~ 33℃ “天气”:“阴”, " weather_id ": { “fa”:“02”, “facebook”:“02” }, “风”:“西南风3 - 4级”, “周”:“星期二”, “日期”:“20150609” }, { “温度”:“22℃~ 33℃ “天气”:“多云”, " weather_id ": { “fa”:“1”, “facebook”:“01” }, “风”:“南风3 - 4级”, “周”:“星期三”, “日期”:“20150610” } ] }, “error_code”: 0 }
进口com.google.gson.JsonObject; 进口com.google.gson.JsonParser; 进口com.google.gson.JsonSyntaxException; 进口com.google.gson.JsonIOException; 进口java.io.FileNotFoundException; 进口java.io.FileReader; 公开课ReadJson { 公共静态void main (String [] args) { JsonParser解析=new JsonParser (); 尝试{ JsonObject json=(JsonObject)解析。解析(新FileReader (“weather.json”)); system . out。println (“resultcode:”+ json.get (resultcodeu) .getAsInt ()); system . out。println(原因:+ json.get(“原因”).getAsString ()); JsonObject结果=json.get(“结果”).getAsJsonObject (); JsonObject今天=result.get(“今天”).getAsJsonObject (); system . out。println(“弱:“+ today.get(“星期”).getAsString ()); system . out。println(“天气:“+ today.get(天气).getAsString ()); }捕捉(JsonIOException e) { e.printStackTrace (); }捕捉(NullPointerException e) { e.printStackTrace (); }捕捉(JsonSyntaxException e) { e.printStackTrace (); }捕捉(FileNotFoundException e) { e.printStackTrace (); } } }Java中利用gson解析Json实例教程