gson 的使用 解析json字符串

private String testStr  = "{\"mSimpleweatherinfo\":{\"city\":\"杭州\",\"cityid\":\"101110101\",\"temp\":\"20\",\"WD\":\"36℃\",\"WS\":\"正常\",\"SD\":\"14%\",\"WSE\":\"1\",\"time\":\"17:00\"}}";
 
下面是定义的两个类   
 
public class SimpleWeather {
   @Expose
   private SimpleWeatherinfo mSimpleweatherinfo;

   public SimpleWeatherinfo getWeatherinfo() {
      return mSimpleweatherinfo;
   }

   public void setWeatherinfo(SimpleWeatherinfo weatherinfo) {
      this.mSimpleweatherinfo = weatherinfo;
   }

}

 
package com.daily.pengshu.happyweather.bean;

import com.google.gson.annotations.Expose;

public class SimpleWeatherinfo {
   @Expose
   private String city;
   @Expose
   private String cityid;
   @Expose
   private String temp;
   @Expose
   private String WD;
   @Expose
   private String WS;
   @Expose
   private String SD;
   @Expose
   private String WSE;
   @Expose
   private String time;

   public String getCity() {
      return city;
   }

   public void setCity(String city) {
      this.city = city;
   }

   public String getCityid() {
      return cityid;
   }

   public void setCityid(String cityid) {
      this.cityid = cityid;
   }

   public String getTemp() {
      return temp;
   }

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

   public String getWD() {
      return WD;
   }

   public void setWD(String wD) {
      WD = wD;
   }

   public String getWS() {
      return WS;
   }

   public void setWS(String wS) {
      WS = wS;
   }

   public String getSD() {
      return SD;
   }

   public void setSD(String sD) {
      SD = sD;
   }

   public String getWSE() {
      return WSE;
   }

   public void setWSE(String wSE) {
      WSE = wSE;
   }

   public String getTime() {
      return time;
   }

   public void setTime(String time) {
      this.time = time;
   }

   @Override
   public String toString() {
      return "SimpleWeatherinfo [city=" + city + ", cityid=" + cityid
            + ", temp=" + temp + ", WD=" + WD + ", WS=" + WS + ", SD=" + SD
            + ", WSE=" + WSE + ", time=" + time + "]";
   }

}
 
Gson mGson = new Gson();
simpleWeather = mGson.fromJson(str,SimpleWeather.class);
simpleWeatherinfo = simpleWeather.getWeatherinfo();


 

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值