Gson的使用

下面例子主要包含两个意思,一个将Json字符串转换为对象
另一个将对象转换为字符串.

获取到服务器传递的Json字符串后,在as中安装插件GsonFormat,按快捷键alt+s,把Json字符串粘贴进去,会自动生成bean对象.

package com.example.administrator.weatherforecast;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

import com.example.administrator.weatherforecast.bean.User;
import com.example.administrator.weatherforecast.bean.Weather;
import com.google.gson.Gson;

import java.util.List;

public class SecondActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        init();
    }

    private void init() {
        findViewById(R.id.second_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                parseData();
            }
        });
        findViewById(R.id.second_btn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                beanToJson();
            }
        });
    }

    /**
     * 将bean转换为Json字符串
     */
    private void beanToJson() {
        User zhangsan = new User(13, "zhangsan");
        Gson gson = new Gson();
        String sGson = gson.toJson(zhangsan);
        System.out.println(sGson);
        /**
         * 06-07 11:07:42.568 20971-20971/com.example.administrator.weatherforecast I/System.out: {"name":"zhangsan","age":13}
         */
    }

    /**
     * 解析json数据
     */
    private void parseData() {
        String jsonstring = "{\"coord\":{\"lon\":116.4,\"lat\":39.91},\"weather\":[{\"id\":800,\"main\":\"Clear\",\"description\":\"clear sky\",\"icon\":\"01d\"}],\"base\":\"stations\",\"main\":{\"temp\":291.15,\"pressure\":1009,\"humidity\":82,\"temp_min\":291.15,\"temp_max\":291.15},\"visibility\":10000,\"wind\":{\"speed\":2,\"deg\":120},\"clouds\":{\"all\":0},\"dt\":1496739600,\"sys\":{\"type\":1,\"id\":7405,\"message\":0.0246,\"country\":\"CN\",\"sunrise\":1496695574,\"sunset\":1496749219},\"id\":1816670,\"name\":\"Beijing\",\"cod\":200}";
        /**
         * {
         * "coord":{"lon":116.4,"lat":39.91},
         * "weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],
         * "base":"stations",
         * "main":{"temp":291.15,"pressure":1009,"humidity":82,"temp_min":291.15,"temp_max":291.15},
         * "visibility":10000,
         * "wind":{"speed":2,"deg":120},
         * "clouds":{"all":0},
         * "dt":1496739600,
         * "sys":{"type":1,"id":7405,"message":0.0246,"country":"CN","sunrise":1496695574,"sunset":1496749219},
         * "id":1816670,
         * "name":"Beijing",
         * "cod":200
         * }
         */
        Gson gson = new Gson();
        Weather weatherBean = gson.fromJson(jsonstring, Weather.class);
        List<Weather.WeatherBean> weather = weatherBean.getWeather();
        Weather.WeatherBean weatherBean1 = weather.get(0);
        String icon = weatherBean1.getIcon();
        System.out.println("icon ="+icon);
        /**
         * 06-07 10:59:03.124 12918-12918/com.example.administrator.weatherforecast I/System.out: icon =01d
         */
    }
}

用工具生成的bean对象

package com.example.administrator.weatherforecast.bean;

import java.util.List;

/**
 * Created by liumeng on 2017/6/7 on 10:52
 */

public class Weather {

    /**
     * lon : 116.4
     * lat : 39.91
     */

    private CoordBean coord;
    /**
     * coord : {"lon":116.4,"lat":39.91}
     * weather : [{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}]
     * base : stations
     * main : {"temp":291.15,"pressure":1009,"humidity":82,"temp_min":291.15,"temp_max":291.15}
     * visibility : 10000
     * wind : {"speed":2,"deg":120}
     * clouds : {"all":0}
     * dt : 1496739600
     * sys : {"type":1,"id":7405,"message":0.0246,"country":"CN","sunrise":1496695574,"sunset":1496749219}
     * id : 1816670
     * name : Beijing
     * cod : 200
     */

    private String    base;
    /**
     * temp : 291.15
     * pressure : 1009
     * humidity : 82
     * temp_min : 291.15
     * temp_max : 291.15
     */

    private MainBean  main;
    private int visibility;
    /**
     * speed : 2
     * deg : 120
     */

    private WindBean   wind;
    /**
     * all : 0
     */

    private CloudsBean clouds;
    private int dt;
    /**
     * type : 1
     * id : 7405
     * message : 0.0246
     * country : CN
     * sunrise : 1496695574
     * sunset : 1496749219
     */

    private SysBean sys;
    private int    id;
    private String name;
    private int    cod;
    /**
     * id : 800
     * main : Clear
     * description : clear sky
     * icon : 01d
     */

    private List<WeatherBean> weather;

    public CoordBean getCoord() {
        return coord;
    }

    public void setCoord(CoordBean coord) {
        this.coord = coord;
    }

    public String getBase() {
        return base;
    }

    public void setBase(String base) {
        this.base = base;
    }

    public MainBean getMain() {
        return main;
    }

    public void setMain(MainBean main) {
        this.main = main;
    }

    public int getVisibility() {
        return visibility;
    }

    public void setVisibility(int visibility) {
        this.visibility = visibility;
    }

    public WindBean getWind() {
        return wind;
    }

    public void setWind(WindBean wind) {
        this.wind = wind;
    }

    public CloudsBean getClouds() {
        return clouds;
    }

    public void setClouds(CloudsBean clouds) {
        this.clouds = clouds;
    }

    public int getDt() {
        return dt;
    }

    public void setDt(int dt) {
        this.dt = dt;
    }

    public SysBean getSys() {
        return sys;
    }

    public void setSys(SysBean sys) {
        this.sys = sys;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getCod() {
        return cod;
    }

    public void setCod(int cod) {
        this.cod = cod;
    }

    public List<WeatherBean> getWeather() {
        return weather;
    }

    public void setWeather(List<WeatherBean> weather) {
        this.weather = weather;
    }

    public static class CoordBean {
        private double lon;
        private double lat;

        public double getLon() {
            return lon;
        }

        public void setLon(double lon) {
            this.lon = lon;
        }

        public double getLat() {
            return lat;
        }

        public void setLat(double lat) {
            this.lat = lat;
        }
    }

    public static class MainBean {
        private double temp;
        private int    pressure;
        private int    humidity;
        private double temp_min;
        private double temp_max;

        public double getTemp() {
            return temp;
        }

        public void setTemp(double temp) {
            this.temp = temp;
        }

        public int getPressure() {
            return pressure;
        }

        public void setPressure(int pressure) {
            this.pressure = pressure;
        }

        public int getHumidity() {
            return humidity;
        }

        public void setHumidity(int humidity) {
            this.humidity = humidity;
        }

        public double getTemp_min() {
            return temp_min;
        }

        public void setTemp_min(double temp_min) {
            this.temp_min = temp_min;
        }

        public double getTemp_max() {
            return temp_max;
        }

        public void setTemp_max(double temp_max) {
            this.temp_max = temp_max;
        }
    }

    public static class WindBean {
        private int speed;
        private int deg;

        public int getSpeed() {
            return speed;
        }

        public void setSpeed(int speed) {
            this.speed = speed;
        }

        public int getDeg() {
            return deg;
        }

        public void setDeg(int deg) {
            this.deg = deg;
        }
    }

    public static class CloudsBean {
        private int all;

        public int getAll() {
            return all;
        }

        public void setAll(int all) {
            this.all = all;
        }
    }

    public static class SysBean {
        private int    type;
        private int    id;
        private double message;
        private String country;
        private int    sunrise;
        private int    sunset;

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public double getMessage() {
            return message;
        }

        public void setMessage(double message) {
            this.message = message;
        }

        public String getCountry() {
            return country;
        }

        public void setCountry(String country) {
            this.country = country;
        }

        public int getSunrise() {
            return sunrise;
        }

        public void setSunrise(int sunrise) {
            this.sunrise = sunrise;
        }

        public int getSunset() {
            return sunset;
        }

        public void setSunset(int sunset) {
            this.sunset = sunset;
        }
    }

    public static class WeatherBean {
        private int    id;
        private String main;
        private String description;
        private String icon;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getMain() {
            return main;
        }

        public void setMain(String main) {
            this.main = main;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public String getIcon() {
            return icon;
        }

        public void setIcon(String icon) {
            this.icon = icon;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值