gson 的使用 解析json字符串

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/ouhuanyixie8627/article/details/51954756
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();




                                    
展开阅读全文

没有更多推荐了,返回首页