GSON get JSON data

GSON is provided by Google for mapping between Java objects and JSON data Java class libraries. Json a character can be transformed into a Java object, or into a Java Json string.
Features:
A, fast, efficient   
B, less code, concise
C, object-oriented
d, data transfer and facilitate parsing

Gon Introduction

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.0</version>
 </dependency>

Gson way to create

  1. Gson gson gson = new ();
  2. By GsonBuilder (), you can be a variety of configurations.
Gson gson = new GsonBuilder()
                        .setLenient()// json宽松  
                        .enableComplexMapKeySerialization()//支持Map的key为复杂对象的形式  
                        .serializeNulls() //智能null  
                        .setPrettyPrinting()// 调教格式  
                        .disableHtmlEscaping() //默认是GSON把HTML 转义的
                        .create(); 

Basic usage of Gson

Weather obtain a written argument before the API, we need to parse the returned json data, as an example.

Get weather parameters

 String url = "http://t.weather.sojson.com/api/weather/city/101010100";
 String resultStr = HttpClientUtil.sendGetRequest(url, "UTF-8");

Return results (not formatted)

{shidu=15%, pm25=15.0, pm10=35.0, quality=优, wendu=3, ganmao=各类人群可自由活动, 
yesterday={date=06, sunrise=07:36, high=高温 3.0℃, low=低温 -7.0℃, sunset=17:03, 
aqi=58.0, ymd=2019-01-06, week=星期日, fx=西南风, fl=<3级, type=晴, 
notice=愿你拥有比阳光明媚的心情}, forecast=[{date=07, sunrise=07:36, high=高温 2.0℃, 
low=低温 -7.0℃, sunset=17:04, aqi=48.0, ymd=2019-01-07, week=星期一, fx=北风, 
fl=3-4级, type=多云, notice=阴晴之间,谨防紫外线侵扰}, {date=08, sunrise=07:36, 
high=高温 1.0℃, low=低温 -9.0℃, sunset=17:05, aqi=28.0, ymd=2019-01-08, week=星期二, 
fx=北风, fl=3-4级, type=晴, notice=愿你拥有比阳光明媚的心情}, {date=09, sunrise=07:36,
 high=高温 2.0℃, low=低温 -8.0℃, sunset=17:06, aqi=83.0, ymd=2019-01-09, week=星期三, 
fx=西南风, fl=<3级, type=多云, notice=阴晴之间,谨防紫外线侵扰}, {date=10, sunrise=07:36, 
high=高温 4.0℃, low=低温 -7.0℃, sunset=17:07, aqi=128.0, ymd=2019-01-10, week=星期四,
 fx=西南风, fl=<3级, type=晴, notice=愿你拥有比阳光明媚的心情}, {date=11, sunrise=07:36, 
high=高温 5.0℃, low=低温 -6.0℃, sunset=17:08, aqi=238.0, ymd=2019-01-11, week=星期五,
 fx=西南风, fl=<3级, type=多云, notice=阴晴之间,谨防紫外线侵扰}]}

The weather can create a Bean, will return json data into objects

//GSON直接解析成对象
ResultBean resultBean = new Gson().fromJson(resultStr,ResultBean.class);

Resolve simple json

data:{
      shidu = 15 % , 
      pm25 = 15.0,
      pm10 = 35.0, 
      quality = 优, 
      wendu = 3, 
      ganmao = 各类人群可自由活动,
     }
JsonObject jsonObject =(JsonObject) new JsonParser().parse(resultStr);
Int wendu = jsonObject.get("data").getAsJsonObject().get("wendu").getAsInt();
String quality= jsonObject.get("data").getAsJsonObject().get("quality").getAsString();

Parsing multilayer objects

 data:{
      shidu = 15 % , 
      pm25 = 15.0, 
      pm10 = 35.0, 
      quality = 优, 
      wendu = 3, 
      ganmao = 各类人群可自由活动, 
      yesterday :{
                    date = 06,
                    sunrise = 07: 36,
                    high = 高温 3.0℃,
                    low = 低温 - 7.0℃,
                    sunset = 17: 03,
                    aqi = 58.0,
                    ymd = 2019 - 01 - 06,
                    week = 星期日,
                    fx = 西南风,
                    fl = < 3 级,
                    type = 晴,
                    notice = 愿你拥有比阳光明媚的心情
                 }
        }

 JsonObject jsonObject = (JsonObject) new JsonParser().parse(resultStr);
 JsonObject yesterday = jsonObject.get("data").getAsJsonObject().get("yesterday ").getAsJsonObject();
 String type  = yesterday.get("type").getAsString();

json parsing with an array of

{
shidu = 15 % , pm25 = 15.0, pm10 = 35.0, quality = 优, wendu = 3, ganmao = 各类人群可自由活动, 
yesterday = {
        date = 06,
        sunrise = 07: 36,
        high = 高温 3.0℃,
        low = 低温 - 7.0℃,
        sunset = 17: 03,
        aqi = 58.0,
        ymd = 2019 - 01 - 06,
        week = 星期日,
        fx = 西南风,
        fl = < 3 级,
        type = 晴,
        notice = 愿你拥有比阳光明媚的心情
    }, 
forecast = [{
        date = 07,
        sunrise = 07: 36,
        high = 高温 2.0℃,
        low = 低温 - 7.0℃,
        sunset = 17: 04,
        aqi = 48.0,
        ymd = 2019 - 01 - 07,
        week = 星期一,
        fx = 北风,
        fl = 3 - 4 级,
        type = 多云,
        notice = 阴晴之间, 谨防紫外线侵扰
    }, {
        date = 08,
        sunrise = 07: 36,
        high = 高温 1.0℃,
        low = 低温 - 9.0℃,
        sunset = 17: 05,
        aqi = 28.0,
        ymd = 2019 - 01 - 08,
        week = 星期二,
        fx = 北风,
        fl = 3 - 4 级,
        type = 晴,
        notice = 愿你拥有比阳光明媚的心情
    }, {
        date = 09,
        sunrise = 07: 36,
        high = 高温 2.0℃,
        low = 低温 - 8.0℃,
        sunset = 17: 06,
        aqi = 83.0,
        ymd = 2019 - 01 - 09,
        week = 星期三,
        fx = 西南风,
        fl = < 3 级,
        type = 多云,
        notice = 阴晴之间, 谨防紫外线侵扰
    }, {
        date = 10,
        sunrise = 07: 36,
        high = 高温 4.0℃,
        low = 低温 - 7.0℃,
        sunset = 17: 07,
        aqi = 128.0,
        ymd = 2019 - 01 - 10,
        week = 星期四,
        fx = 西南风,
        fl = < 3 级,
        type = 晴,
        notice = 愿你拥有比阳光明媚的心情
    }, {
        date = 11,
        sunrise = 07: 36,
        high = 高温 5.0℃,
        low = 低温 - 6.0℃,
        sunset = 17: 08,
        aqi = 238.0,
        ymd = 2019 - 01 - 11,
        week = 星期五,
        fx = 西南风,
        fl = < 3 级,
        type = 多云,
        notice = 阴晴之间, 谨防紫外线侵扰
    }]
}

JsonObject jsonObject =(JsonObject) new JsonParser().parse(resultStr);
//获取data
JsonObject data = jsonObject.get("data").getAsJsonObject();
//获取数组
JsonArray forecast = data.getAsJsonObject().get("forecast").getAsJsonArray();
String type  = forecast.get(0).getAsJsonObject().get("type").getAsString();

Personal blog
Tencent cloud community
Nuggets
Jane book
blog Park
GitHub
code cloud
Segmentfault
public number:wx.jpg

Published 19 original articles · won praise 1 · views 432

Guess you like

Origin blog.csdn.net/Devilli0310/article/details/87876858