前言
本文主要跟大家介绍了关于爪哇用gson解析Json的相关内容,分享出来供大家参考学习,需要的朋友们下面来一起看看吧。
json数据
{“结果代码”:“200”,“原因”:成功!",结果: { 'sk': { 'temp': '24 ',' wind_direction': '西南风,'风力' : '2级,'湿度' : '51% ','时间' : ' 10336011 ' },'今天' : { '温度' : '16~27','天气' : '阴转多云,' weather_id': { 'fa': '02 ',' fb': '01' },' wind': '西南风3-4 级,'周' : '星期四,'城市' :滨州,' date_y': '2015年06月04日,'敷料_索引' : '舒适,'穿衣建议' : '建议着长袖T恤、衬衫加单裤等服装。年老体弱者宜着针织长袖衬衫、马甲和长裤','紫外线指数' : '最弱,'舒适指数' : ',洗涤指数' : '较适宜,' travel_index': ' ',' execution _ index ' : '较适宜,'烘干_指数' : '' },'未来' : [ { '温度' : '16~27','天气' : '阴转多云,' weather_id': { 'fa': '02 ',' fb': '01' },' wind': '西南风3-4 级,'周' : '星期四,'日期' : '20150604' },{ '温度' : '20~32', 天气' : '多云转晴,' weather_id': { 'fa': '01 ',' fb': '00' },' wind': '西风3-4 级,'周' : '星期五,'日期' : '20150605' },{ '温度' : '23~35','天气' : '多云转阴,' weather_id': { 'fa': '01 ',' fb': '02' },' wind': '西南风3-4 级,'周' : '星期六,'日期' : '20150606' },{ '温度' : '20~33','天气' : '多云,' weather_id': { 'fa': '01 ',' fb': '01' },' wind': '北风微风,'周' : '星期日,'日期' : '20150607' },{ '温度' : '22~34','天气' : '多云,' weather_id': { 'fa': '01 ',' fb': '01' },' wind': '西南风3-4 级,'周' : '星期一,'日期' : '20150608' },{ '温度' : '22~33','天气' : '阴,weather_id': { 'fa': '02 ',' fb': '02' },' wind': '西南风3-4 级,'周' : '星期二,'日期' : '20150609' },{ '温度' : '22~33','天气' : '多云,' weather_id': { 'fa': '01 ',' fb': '01' },' wind': '南风3-4 级,'周' : '星期三,' date': '20150610' } ] },' error_code': 0}解析JSONObject
导入com。谷歌。gson。JSON对象;导入com。谷歌。gson。jsonparser导入com。谷歌。gson。jsonsynctaxception导入com。谷歌。gson。jsonioeexception导入Java。io。FileNotFoundException导入Java。io。FileReader公共类ReadJson { public static void main(String[]args){ JsonParser parse=new JsonParser();尝试{ JSON对象JSON=(JSON对象)解析。解析(新文件阅读器(' weather。JSON’);系统。出去。println(' resultcode : ' JSON。get(' resultcodeu ').getAsInt());系统。出去。println('原因: ' JSON。得到(原因).getAsString());JSON对象结果=JSON。获取('结果').getAsJsonObject();今日JSON对象=结果。get('今日').getAsJsonObject();系统。出去。今日println('弱3360 '。获取('周').getAsString());系统。出去。今天天气:天气预报.getAsString());} catch(JSonioexception e){ e . print stack trace();} catch(NullPointRexception e){ e . print stack trace();} catch(jsonsync exception e){ e . printstacktrace();} catch(FileNotFoundException e){ e . printStackTrace();} }}解析JSONArray
导入com。谷歌。gson。jsonparser进口。com。谷歌。gson。jsonarray导入com。谷歌。gson。JSON对象;导入com。谷歌。gson。jsonsynctaxception导入com。谷歌。gson。jsonioeexception导入Java。io。FileNotFoundException导入Java。io。FileReader公共类ReadJsonArray { public static void main(String[]args){ JsonParser parse=new JsonParser();尝试{ JSON对象JSON=(JSON对象)解析。解析(新的文件阅读器(' c : Users wzh 94434 idea project test project jsontest src main Java weather。JSON’);JSON对象结果=JSON。获取('结果').getAsJsonObject();JSON数组Futurearray=结果。获取(未来).getAsJsonArray();for(int I=0;我是未来光线。size();I){ JSON对象subbj=futurearray。得到(我).getAsJsonObject();系统。出去。println('-');系统。出去。println('第:周' SubBj。获取('周').getAsString());系统。出去。println(' weather : ' Subbbj。天气预报.getAsString());} } catch(FileNotFoundException e){ e . printStackTrace();} catch(JSonioexception e){ e . print stack trace();} catch(jsonsync exception e){ e . printstacktrace();} }}注意:文件路径相对路径是从工程根目录开始
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对我们的支持。