关于JSONObject解析json

前端之家收集整理的这篇文章主要介绍了关于JSONObject解析json前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

如果没有list只是object的话,用JSONObject解析接送也是个不错的选择
今天的测试数据是

{
    "reason": "查询成功!","result": { "data": { "realtime": { "city_code": "101110801","city_name": "汉中","date": "2017-11-04","time": "21:00:00","week": 6,"moon": "九月十六","dataUptime": 1509801446,"weather": { "temperature": "13","humidity": "99","info": "晴","img": "0" },"wind": { "direct": "西南风","power": "1级","offset": null,"windspeed": null } },"life": { "date": "2017-11-4","info": { "chuanyi": [ "较舒适","建议着薄外套、开衫牛仔衫裤等服装。年老体弱者应适当添加衣物,宜着夹克衫、薄毛衣等。" ],"ganmao": [ "较易发","昼夜温差较大,较易发生感冒,请适当增减衣服。体质较弱的朋友请注意防护。" ],"kongtiao": [ "较少开启","您将感到很舒适,一般不需要开启空调。" ],"xiche": [ "较适宜","较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。" ],"yundong": [ "适宜","天气较好,赶快投身大自然参与户外运动,尽情感受运动的快乐吧。" ],"ziwaixian": [ "弱","紫外线强度较弱,建议出门前涂擦SPF在12-15之间、PA+的防晒护肤品。" ] } },"weather": [ { "date": "2017-11-04","info": { "day": [ "1","多云","19","东北风","微风","07:12" ],"night": [ "0","晴","10","17:58" ] },"week": "六","nongli": "九月十六" },{ "date": "2017-11-05","info": { "dawn": [ "0","17:58" ],"day": [ "2","阴","16","07:13" ],"night": [ "2","东风","17:57" ] },"week": "日","nongli": "九月十七" },{ "date": "2017-11-06","info": { "dawn": [ "2","17:57" ],"day": [ "1","20","西风","07:14" ],"night": [ "1","17:56" ] },"week": "一","nongli": "九月十八" },{ "date": "2017-11-07","info": { "dawn": [ "1","17:56" ],"day": [ "0","07:15" ],"11","17:55" ] },"week": "二","nongli": "九月十九" },{ "date": "2017-11-08","17:55" ],"07:16" ],"night": [ "7","小雨","东南风","week": "三","nongli": "九月二十" },{ "date": "2017-11-09","info": { "night": [ "1","8","19:30" ],"day": [ "3","阵雨","07:30" ] },"week": "四","nongli": "九月廿一" },{ "date": "2017-11-10","info": { "night": [ "3","7","15","week": "五","nongli": "九月廿二" } ],"f3h": { "temperature": [ { "jg": "20171104200000","jb": "13" },{ "jg": "20171104230000","jb": "12" },{ "jg": "20171105020000","jb": "11" },{ "jg": "20171105050000","jb": "10" },{ "jg": "20171105080000",{ "jg": "20171105110000","jb": "14" },{ "jg": "20171105140000","jb": "15" },{ "jg": "20171105170000","jb": "16" },{ "jg": "20171105200000","jb": "13" } ],"precipitation": [ { "jg": "20171104200000","jf": "0" },"jf": "0" } ] },"pm25": { "key": "Hanzhong","show_desc": 0,"pm25": { "curPm": "90","pm25": "67","pm10": "117","level": 2,"quality": "良","des": "可以正常在户外活动,易敏感人群应减少外出" },"dateTime": "2017年11月04日20时","cityName": "汉中" },"jingqu": "","jingqutq": "","date": "","isForeign": "0","partner": { "title_word": "中国","show_url": "www.weather.com.cn","base_url": "http:\/\/j.www.haosou.com\/?u=http%3A%2F%2Fwww.weather.com.cn%2Fweather%2F101110801.shtml&m=073ae0&from=juhe&type=weather" } } },"error_code": 0 }

这是聚合数据返回的汉中市天气的数据

可能是因为中文太多了或者这个数据结构中空格太多了,我们的歌颂format无法自动生成实体类

JSONObject jsonObject = new JSONObject(string);
String result = jsonObject.getString("result");
JSONObject jsonObject1 = new JSONObject(result);
data = jsonObject1.getString("data");
JSONObject jsonObject2 = new JSONObject(data);
 @H_301_757@//当前时间的天气情况
realtime = jsonObject2.getString("realtime");
JSONObject object = new JSONObject(realtime);
tvCity.setText(object.getString("city_name"));
 tvTemp.setText(new JSONObject(object.getString("weather")).getString("temperature") + "℃");
String weatherInfo = new JSONObject(object.getString("weather")).getString("info");
tvWeather.setText(weatherInfo);

突然发现以前的项目中还有
JsonArray
于是找了下,没有打印json找了下,文档看了下数据结构

Gson gson=new Gson();
Type type=new TypeToken<JsonObject>(){}.getType();
JsonObject object=gson.fromJson(jsonMedicalAddress,type);
Log.i("jsonMedicalAddress",jsonMedicalAddress);
JsonObject object1=object.getAsJsonObject("data");
JsonArray jsonArray=object1.getAsJsonArray("附近医院");
                                    @H_301_757@// 按照医院距离列表的迭代器
                                    Iterator it=jsonArray.iterator();
                                    while (it.hasNext()){
                                        JsonElement e=(JsonElement)it.next();
                                        @H_301_757@//JsonElement转换为JavaBean
                                        @H_301_757@// 医院实体类 Medical
                                        Medical medical=gson.fromJson(e,Medical.class);
                                        list.add(medical);
                                    }
                                    if(list.size()!=0){
                                        Log.i("hospitals.size",list.toString());
                                        hospitalAdapter=new HospitalAdapter(list,getApplicationContext(),lng,lat,true);
                                        listView.setAdapter(hospitalAdapter);
                                    }

业精于勤荒于嬉

猜你在找的Json相关文章