Gson quickly parses Json array

This is a framework I had to document to quickly parse complex Json arrays to prevent forgetting in future work. (Complex focus, simple json data can be completely parsed with JsonObject)

Example:

{
  "HeWeather6": [
    {
      "basic": {
        "cid": "CN101280601",
        "location": "shenzhen",
        "parent_city": "shenzhen",
        "admin_area": "guangdong",
        "cnty": "China",
        "lat": "22.54700089",
        "lon": "114.08594513",
        "tz": "+8.00"
      },
      "update": {
        "loc": "2018-04-11 09:47",
        "utc": "2018-04-11 01:47"
      },
      "status": "ok",
      "now": {
        "cloud": "75",
        "cond_code": "101",
        "cond_txt": "Cloudy",
        "fl": "27",
        "hum": "74",
        "pcpn": "0.0",
        "pres": "1014",
        "tmp": "25",
        "vis": "10",
        "wind_deg": "100",
        "wind_dir": "E",
        "wind_sc": "1",
        "wind_spd": "2"
      },
      "daily_forecast": [
        {
          "cond_code_d": "101",
          "cond_code_n": "101",
          "cond_txt_d": "Cloudy",
          "cond_txt_n": "Cloudy",
          "date": "2018-04-11",
          "hum": "77",
          "mr": "02:58",
          "ms": "14:26",
          "pcpn": "0.0",
          "pop": "0",
          "pres": "1013",
          "sr": "06:07",
          "ss": "18:42",
          "tmp_max": "29",
          "tmp_min": "22",
          "uv_index": "11",
          "vis": "20",
          "wind_deg": "0",
          "wind_dir": "no direction",
          "wind_sc": "1-2",
          "wind_spd": "1"
        },
        {
          "cond_code_d": "101",
          "cond_code_n": "101",
          "cond_txt_d": "Cloudy",
          "cond_txt_n": "Cloudy",
          "date": "2018-04-12",
          "hum": "85",
          "mr": "03:39",
          "ms": "15:19",
          "pcpn": "0.0",
          "pop": "0",
          "pres": "1011",
          "sr": "06:06",
          "ss": "18:43",
          "tmp_max": "29",
          "tmp_min": "23",
          "uv_index": "11",
          "vis": "16",
          "wind_deg": "0",
          "wind_dir": "no direction",
          "wind_sc": "1-2",
          "wind_spd": "3"
        },
        {
          "cond_code_d": "101",
          "cond_code_n": "101",
          "cond_txt_d": "Cloudy",
          "cond_txt_n": "Cloudy",
          "date": "2018-04-13",
          "hum": "82",
          "mr": "04:17",
          "ms": "16:12",
          "pcpn": "0.0",
          "pop": "0",
          "pres": "1012",
          "sr": "06:06",
          "ss": "18:43",
          "tmp_max": "29",
          "tmp_min": "23",
          "uv_index": "12",
          "vis": "16",
          "wind_deg": "0",
          "wind_dir": "no direction",
          "wind_sc": "1-2",
          "wind_spd": "9"
        }
      ],
      "hourly": [
        {
          "cloud": "38",
          "cond_code": "103",
          "cond_txt": "Partly Cloudy",
          "dew": "21",
          "hum": "68",
          "pop": "1",
          "pres": "1014",
          "time": "2018-04-11 10:00",
          "tmp": "25",
          "wind_deg": "147",
          "wind_dir": "SE",
          "wind_sc": "1-2",
          "wind_spd": "8"
        },
        {
          "cloud": "18",
          "cond_code": "103",
          "cond_txt": "Partly Cloudy",
          "dew": "18",
          "hum": "52",
          "pop": "0",
          "pres": "1012",
          "time": "2018-04-11 13:00",
          "tmp": "27",
          "wind_deg": "174",
          "wind_dir": "S",
          "wind_sc": "1-2",
          "wind_spd": "4"
        },
        {
          "cloud": "5",
          "cond_code": "103",
          "cond_txt": "Partly Cloudy",
          "dew": "18",
          "hum": "56",
          "pop": "0",
          "pres": "1010",
          "time": "2018-04-11 16:00",
          "tmp": "28",
          "wind_deg": "178",
          "wind_dir": "S",
          "wind_sc": "1-2",
          "wind_spd": "5"
        },
        {
          "cloud": "2",
          "cond_code": "103",
          "cond_txt": "Partly Cloudy",
          "dew": "21",
          "hum": "79",
          "pop": "0",
          "pres": "1011",
          "time": "2018-04-11 19:00",
          "tmp": "27",
          "wind_deg": "170",
          "wind_dir": "S",
          "wind_sc": "1-2",
          "wind_spd": "5"
        },
        {
          "cloud": "3",
          "cond_code": "103",
          "cond_txt": "Partly Cloudy",
          "dew": "21",
          "hum": "91",
          "pop": "0",
          "pres": "1012",
          "time": "2018-04-11 22:00",
          "tmp": "23",
          "wind_deg": "155",
          "wind_dir": "SE",
          "wind_sc": "1-2",
          "wind_spd": "10"
        },
        {
          "cloud": "62",
          "cond_code": "103",
          "cond_txt": "Partly Cloudy",
          "dew": "21",
          "hum": "91",
          "pop": "2",
          "pres": "1011",
          "time": "2018-04-12 01:00",
          "tmp": "22",
          "wind_deg": "140",
          "wind_dir": "SE",
          "wind_sc": "1-2",
          "wind_spd": "10"
        },
        {
          "cloud": "96",
          "cond_code": "103",
          "cond_txt": "Partly Cloudy",
          "dew": "22",
          "hum": "94",
          "pop": "2",
          "pres": "1010",
          "time": "2018-04-12 04:00",
          "tmp": "22",
          "wind_deg": "133",
          "wind_dir": "SE",
          "wind_sc": "1-2",
          "wind_spd": "3"
        },
        {
          "cloud": "99",
          "cond_code": "103",
          "cond_txt": "Partly Cloudy",
          "dew": "22",
          "hum": "91",
          "pop": "7",
          "pres": "1011",
          "time": "2018-04-12 07:00",
          "tmp": "22",
          "wind_deg": "140",
          "wind_dir": "SE",
          "wind_sc": "1-2",
          "wind_spd": "1"
        }
      ],
      "lifestyle": [
        {
          "brf": "more comfortable",
          "txt": "The weather is fine during the day. Under such weather conditions, you will feel cool and comfortable in the morning and evening, and warm in the afternoon.",
          "type": "comf"
        },
        {
          "brf": "Comfort",
          "txt": "It is recommended to wear long-sleeved T-shirts, shirts and trousers. The elderly and infirm are advised to wear knitted long-sleeved shirts, vests and trousers.",
          "type": "drsg"
        },
        {
          "brf": "少发",
          "txt": "The weather conditions are suitable, there is no obvious cooling process, and the chance of catching a cold is low.",
          "type": "flu"
        },
        {
          "brf": "suitable",
          "txt": "The weather is good, so hurry up and join nature to participate in outdoor sports and enjoy the joy of sports.",
          "type": "sport"
        },
        {
          "brf": "suitable",
          "txt": "The weather is good, but it will not affect your mood of travel at all. The temperature is suitable and the breeze is accompanied, suitable for travel.",
          "type": "trav"
        },
        {
          "brf": "弱",
          "txt": "The intensity of UV rays is weak. It is recommended to apply sunscreen skin care products with SPF between 12-15 and PA+ before going out.",
          "type": "uv"
        },
        {
          "brf": "more suitable",
          "txt": "It is more suitable for car washing. There will be no rain in the next day, and the wind will be less. A new car can be kept for at least one day.",
          "type": "cw"
        },
        {
          "brf": "中",
          "txt": "Meteorological conditions have no significant effect on the dilution, diffusion and removal of air pollutants, and susceptible people should appropriately reduce outdoor activity time.",
          "type": "air"
        }
      ]
    }
  ]
}

For the above Json data with many Lists nested in the Object, it is easy to parse it with Gson. Sorry, I forced myself to play again. Without further ado, let's face the wind! ! !

1. Create a Bean. As we all know, the most difficult thing in parsing Json is to establish the corresponding entity class. It needs to correspond to our data one by one to get the json data correctly.

You deserve the GsonFormat plugin. (Extra large font is used for emphasis)


2. When the Response we requested is converted into String (not to mention the network request, it is not the focus of this record)

Gson gson = new Gson ();
final WeatherModel weatherModel = gson.fromJson(response,WeatherModel.class);

3. Get the corresponding data

天气状况:string_weather_icon_code = weatherModel.getHeWeather6().get(0).getNow().getCond_code();

Today's weather and living index:

tv_weather_title.setText(weatherModel.getHeWeather6().get(0).getNow().getCond_txt());
tv_hi_temp.setText(weatherModel.getHeWeather6().get(0).getDaily_forecast().get(0).getTmp_max()+"");
tv_lo_temp.setText(weatherModel.getHeWeather6().get(0).getDaily_forecast().get(0).getTmp_min()+"");
tv_weather_hum_content.setText(weatherModel.getHeWeather6().get(0).getNow().getHum());
tv_weather_wind_content.setText(weatherModel.getHeWeather6().get(0).getNow().getWind_sc()+"");
tv_weather_lifestyle.setText("生活指数:"+weatherModel.getHeWeather6().get(0).getLifestyle().get(1).getTxt());

Really So Easy! ! ! ! !

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325565916&siteId=291194637