使用Gson解析复杂的json数据

时间:2025-01-27 12:25:43


             很多时候大家都是不知道这个Bean是该怎么定义,这里面需要注意几点:
             1、内部嵌套的类必须是static的,要不然解析会出错;
             2、类里面的属性名必须跟Json字段里面的Key是一模一样的;
             3、内部嵌套的用[]括起来的部分是一个List,所以定义为 public List<B> b,而只用{}嵌套的就定义为 public C c,
                  具体的大家对照Json字符串看看就明白了,不明白的我们可以互相交流,本人也是开发新手!
 

             Gson gson = new Gson();
              type = new TypeToken<JsonBean>() {}.getType();
             JsonBean jsonBean = (json, type);</span>


       然后想拿数据就很简单啦,直接在jsonBean里面取就可以了!
       如果需要解析的Json嵌套了很多层,同样可以可以定义一个嵌套很多层内部类的Bean,需要细心的对照Json字段来定义哦。

       下面我将以一个具体的列子来说明通过Gson方式解析复杂的json数据
1.将要解析的数据如下面的格式

{
    "error": 0,
    "status": "success",
    "date": "2014-05-10",
    "results": [
        {
            "currentCity": "南京",
            "weather_data": [
                {
                    "date": "周六(今天, 实时:19℃)",
                    "dayPictureUrl": "/images/weather/day/",
                    "nightPictureUrl": "/images/weather/night/",
                    "weather": "大雨",
                    "wind": "东南风5-6级",
                    "temperature": "18℃"
                },
                {
                    "date": "周日",
                    "dayPictureUrl": "/images/weather/day/",
                    "nightPictureUrl": "/images/weather/night/",
                    "weather": "阵雨转多云",
                    "wind": "西北风4-5级",
                    "temperature": "21 ~ 14℃"
                }
            ]
        }
    ]
}
2.必须定义如下一些的javaBean数据

public class Status 
{
        private String error;
        private String status;
        private String date;
        private List<Results> results;
		public String getError() 
		{
			return error;
		}
		public void setError(String error) 
		{
			 = error;
		}
		
		public String getStatus() 
		{
			return status;
		}
		public void setStatus(String status) 
		{
			 = status;
		}
		public String getDate() 
		{
			return date;
		}
		public void setDate(String date) 
		{
			 = date;
		}
		public List<Results> getResults() 
		{
			return results;
		}
		public void setResults(List<Results> results) 
		{
			 = results;
		}
		@Override
		public String toString() 
		{
			return "Status [error=" + error + ", status=" + status
					+ ", date=" + date + ", results=" + results + "]";
		}
</span>

public class Results 
{	
	private String currentCity;
	private List<Weather> weather_data;
	public String getCurrentCity() 
	{
		return currentCity;
	}
	public void setCurrentCity(String currentCity) 
	{
		 = currentCity;
	}
	public List<Weather> getWeather_data() 
	{
		return weather_data;
	}
	public void setWeather_data(List<Weather> weather_data) 
	{
		this.weather_data = weather_data;
	}
	@Override
	public String toString() 
	{
		return "Results [currentCity=" + currentCity + ", weather_data="
				+ weather_data + "]";
	}


public class Weather {
	private String date;
		        private String dayPictureUrl;
		        private String nightPictureUrl;
		        private String weather;
		        private String wind;
		        private String temperature;
				public String getDate() {
					return date;
				}
				public void setDate(String date) {
					 = date;
				}
				public String getDayPictureUrl() {
					return dayPictureUrl;
				}
				public void setDayPictureUrl(String dayPictureUrl) {
					 = dayPictureUrl;
				}
				public String getNightPictureUrl() {
					return nightPictureUrl;
				}
				public void setNightPictureUrl(String nightPictureUrl) {
					 = nightPictureUrl;
				}
				public String getWeather() {
					return weather;
				}
				public void setWeather(String weather) {
					 = weather;
				}
				public String getWind() {
					return wind;
				}
				public void setWind(String wind) {
					 = wind;
				}
				public String getTemperature() {
					return temperature;
				}
				public void setTemperature(String temperature) {
					 = temperature;
				}
				@Override
				public String toString() {
					return "Weather [date=" + date + ", dayPictureUrl="
							+ dayPictureUrl + ", nightPictureUrl="
							+ nightPictureUrl + ", weather=" + weather
							+ ", wind=" + wind + ", temperature=" + temperature
							+ "]";
				} 
		      
然后具体的javabean定义好了就将解析数据了,下面就是我的解析数据类
public class MainActivity extends Activity 
{
	private Button tojson;	
	RequestQueue mQueue;	
	StringRequest stringRequest;	
	Gson gson;
	String str;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        (savedInstanceState);
        setContentView(.activity_main);
        
        tojson = (Button)findViewById();
        gson = new Gson();

	mQueue = (); 
         //http://10.19.20.12/upgrade/是测试使用的json数据
        stringRequest = new StringRequest("http://10.19.20.12/upgrade/",
				new <String>() 
				{
					@Override
					public void onResponse(String response) 
					{
						("TAG", response);
						("response="+response);
						Status status = (response, );
						("status="+status);
						("-------------------------------------");
						List<Results> result = ();
						("result="+result);
						
					}
				}, 
				new () 
				{
					@Override
					public void onErrorResponse(VolleyError error) 
					{
						("TAG", (), error);
					}

				});
  
        (new OnClickListener() 
        {		
			@Override
			public void onClick(View v) 
			{
				(stringRequest);
			}
		});     
    }

    
 
}
其中上面的RequestQueue是开源网络库Volley的使用,如果你对该库的使用还不熟悉的话可以参考 /guolin_blog/article/details/17482095 ,该作者对Volley库的使用讲解得非常的细致和深入
大家可以仔细的去拜读。