前端之家收集整理的这篇文章主要介绍了
jsonobject解析数据,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@Override public void onSuccess(int statusCode,String responseString) { // TODO Auto-generated method stub // tv.append(responseString + "\n"); try { JSONObject jsonObject = new JSONObject( responseString); reason = jsonObject.getString("reason"); JSONObject result = jsonObject .getJSONObject("result"); JSONObject data = result.getJSONObject("data"); JSONObject realtime = data .getJSONObject("realtime"); JSONObject life = data.getJSONObject("life"); JSONArray weather_array = data .getJSONArray("weather"); pm25 = data.getString("pm25"); date = life.getString("date"); JSONObject info = life.getJSONObject("info"); chuanyi = info.getString("chuanyi"); ganmao = info.getString("ganmao");// 感冒 kongtiao = info.getString("kongtiao"); wuran = info.getString("wuran");// xiche = info.getString("xiche"); yundong = info.getString("yundong"); ziwaixian = info.getString("ziwaixian"); city_name = realtime.getString("city_name"); city_code = realtime.getString("city_code"); time = realtime.getString("time"); moon = realtime.getString("moon"); JSONObject weather = realtime .getJSONObject("weather"); JSONObject wind = realtime.getJSONObject("wind"); direct = wind.getString("direct"); power = wind.getString("power"); temperature = weather.getString("temperature"); humidity = weather.getString("humidity"); weather_info = weather.getString("info"); img = weather.getString("img"); JSONObject weather0 = (JSONObject) weather_array .get(0); JSONObject weather1 = (JSONObject) weather_array .get(1); JSONObject weather2 = (JSONObject) weather_array .get(2); JSONObject weather3 = (JSONObject) weather_array .get(3); JSONObject weather4 = (JSONObject) weather_array .get(4); JSONObject weather5 = (JSONObject) weather_array .get(5); JSONObject weather6 = (JSONObject) weather_array .get(6); date0 = weather0.getString("date"); JSONObject info0 = weather0.getJSONObject("info"); day0 = info0.getString("day"); night0 = info0.getString("night"); week0 = weather0.getString("week"); nongli0 = weather0.getString("nongli"); date1 = weather1.getString("date"); JSONObject info1 = weather1.getJSONObject("info"); day1 = info1.getString("day"); night1 = info1.getString("night"); week1 = weather1.getString("week"); nongli1 = weather1.getString("nongli"); date2 = weather2.getString("date"); JSONObject info2 = weather2.getJSONObject("info"); day2 = info2.getString("day"); night2 = info2.getString("night"); week2 = weather2.getString("week"); nongli2 = weather2.getString("nongli"); date3 = weather3.getString("date"); JSONObject info3 = weather3.getJSONObject("info"); day3 = info3.getString("day"); night3 = info3.getString("night"); week3 = weather3.getString("week"); nongli3 = weather3.getString("nongli"); date4 = weather4.getString("date"); JSONObject info4 = weather4.getJSONObject("info"); day4 = info4.getString("day"); night4 = info4.getString("night"); week4 = weather4.getString("week"); nongli4 = weather4.getString("nongli"); date5 = weather5.getString("date"); JSONObject info5 = weather5.getJSONObject("info"); day5 = info5.getString("day"); night5 = info5.getString("night"); week5 = weather5.getString("week"); nongli5 = weather5.getString("nongli"); date6 = weather6.getString("date"); JSONObject info6 = weather6.getJSONObject("info"); day6 = info6.getString("day"); night6 = info6.getString("night"); week6 = weather6.getString("week"); nongli6 = weather6.getString("nongli"); //利用正则表达式,去掉特殊字符 String pattern = "([-+*/^()\\]\\[])"; str_chuangyi = chuanyi.replaceAll(pattern,""); str_ganmao = ganmao.replaceAll(pattern,""); str_kongtiao = kongtiao.replaceAll(pattern,""); str_wuran = wuran.replaceAll(pattern,""); str_xiche = xiche.replaceAll(pattern,""); str_yundong = yundong.replaceAll(pattern,""); str_ziwaixian = ziwaixian.replaceAll(pattern,""); str_day0 = day0.replaceAll(pattern,""); str_day1 = day1.replaceAll(pattern,""); str_day2 = day2.replaceAll(pattern,""); str_day3 = day3.replaceAll(pattern,""); str_day4 = day4.replaceAll(pattern,""); str_day5 = day5.replaceAll(pattern,""); str_day6 = day6.replaceAll(pattern,""); str_night0 = night0.replaceAll(pattern,""); str_night1 = night1.replaceAll(pattern,""); str_night2 = night2.replaceAll(pattern,""); str_night3 = night3.replaceAll(pattern,""); str_night4 = night4.replaceAll(pattern,""); str_night5 = night5.replaceAll(pattern,""); str_night6 = night6.replaceAll(pattern,""); String[] arr_day0 = str_day0.split(","); String[] arr_day1 = str_day1.split(","); String[] arr_day2 = str_day2.split(","); String[] arr_day3 = str_day3.split(","); String[] arr_day4 = str_day4.split(","); String[] arr_day5 = str_day5.split(","); String[] arr_day6 = str_day6.split(","); String[] arr_night0 = str_night0.split(","); String[] arr_night1 = str_night1.split(","); String[] arr_night2 = str_night2.split(","); String[] arr_night3 = str_night3.split(","); String[] arr_night4 = str_night4.split(","); String[] arr_night5 = str_night5.split(","); String[] arr_night6 = str_night6.split(","); System.out.println("aaaaaaaaaaa"+arr_day0[3]+":"+arr_day6[2]); // 将请求数据
添加进TextView里面 tab01_title.append(week0); tab01_info.append(weather_info); tv_info.append(weather_info); tab01_time.append(time); tab01_temperature.append(temperature+"℃"); tab01_humidity.append(humidity+"%"); tab01_direct.append(direct); tab01_power.append(power); /*tab01_chuanyi.append(chuanyi); tab01_ganmao.append(ganmao); tab01_wuran.append(wuran); tab01_xiche.append(xiche); tab01_yundong.append(yundong); tab01_ziwaixian.append(ziwaixian); tab01_kongtiao.append(kongtiao);*/ tab01_weather_date01.append(date0); tab01_weather_week01.append(week0); tab01_weather_nongli01.append(nongli0); tab01_weather_day01.append(arr_day0[1]+" "+arr_day0[2]+"℃"+" "+arr_day0[4]); tab01_weather_night01.append(arr_night0[1]+" "+arr_night0[2]+"℃"+" "+arr_night0[4]); tab01_weather_date02.append(date1); tab01_weather_day02.append(arr_day1[1]+" "+arr_day1[2]+"℃"+" "+arr_day1[4]); tab01_weather_night02.append(arr_night1[1]+" "+arr_night1[2]+"℃"+" "+arr_night1[4]); tab01_weather_week02.append(week1); tab01_weather_nongli02.append(nongli1); tab01_weather_date03.append(date2); tab01_weather_day03.append(arr_day2[1]+" "+arr_day2[2]+"℃"+" "+arr_day2[4]); tab01_weather_night03.append(arr_night2[1]+" "+arr_night2[2]+"℃"+" "+arr_night2[4]); tab01_weather_week03.append(week2); tab01_weather_nongli03.append(nongli2); tab01_weather_date04.append(date3); tab01_weather_day04.append(arr_day3[1]+" "+arr_day3[2]+"℃"+" "+arr_day3[4]); tab01_weather_night04.append(arr_night3[1]+" "+arr_night3[2]+"℃"+" "+arr_night3[4]); tab01_weather_week04.append(week3); tab01_weather_nongli04.append(nongli3); tab01_weather_date05.append(date4); tab01_weather_day05.append(arr_day4[1]+" "+arr_day4[2]+"℃"+" "+arr_day4[4]); tab01_weather_night05.append(arr_night4[1]+" "+arr_night4[2]+"℃"+" "+arr_night4[4]); tab01_weather_week05.append(week4); tab01_weather_nongli05.append(nongli4); tab01_weather_date06.append(date5); tab01_weather_day06.append(arr_day5[1]+" "+arr_day5[2]+"℃"+" "+arr_day5[4]); tab01_weather_night06.append(arr_night5[1]+" "+arr_night5[2]+"℃"+" "+arr_night5[4]); tab01_weather_week06.append(week5); tab01_weather_nongli06.append(nongli5); tab01_weather_date07.append(date6); tab01_weather_day07.append(arr_day6[1]+" "+arr_day6[2]+"℃"+" "+arr_day6[4]); tab01_weather_night07.append(arr_night6[1]+" "+arr_night6[2]+"℃"+" "+arr_night6[4]); tab01_weather_week07.append(week6); tab01_weather_nongli07.append(nongli6); int intimg = Integer.parseInt(img);//字符串转化为整数 //
显示天气图标 if(intimg == 0){ tab01_air.setBackgroundResource(R.drawable.gif0); }else if(intimg == 1){ tab01_air.setBackgroundResource(R.drawable.gif1); }else if(intimg == 2){ tab01_air.setBackgroundResource(R.drawable.gif2); }else if(intimg == 3){
原文链接:https://www.f2er.com/json/289752.html