Android開發之Gson解析Json嵌套資料
Gson解析複雜的json資料
在這裡介紹解析json資料的另外一種方法就是通過Gson解析,對於解析比較簡單的json資料我就不介紹了來一個比較複雜一點的json資料,如下面我們要解析的一個json資料:
[java]
- String json = {a:100,b:[{b1:b_value1,b2:b_value2}, {b1:b_value1,b2:b_value2}],c: {c1:c_value1,c2:c_value2}}
如果使用JsonObject和JsonArray的配合起來使用也是可以解析的但是解析起來就比較麻煩了,如果使用Gson解析就比較簡單了,首先我們需要定義一個序列化的Bean,這裡採用內部類的形式,這樣比較容易看得清晰些
首先我們需要定義一個序列化的Bean,這裡採用內部類的形式,看起來會比較清晰一些:
[java]
- public class JsonBean {
- public String a;
- public List b;
- public C c;
-
- public static class B {
-
- public String b1;
-
- public String b2;
- }
-
- public static class C {
- public String c1;
- public String c2;
- }
- }
很多時候大家都是不知道這個Bean是該怎麼定義,這裡面需要注意幾點:
1、內部嵌套的類必須是static的,要不然解析會出錯;
2、類裡面的屬性名稱必須跟Json欄位裡面的Key是一模一樣的;
3、內部嵌套的用[]括起來的部分是一個List,所以定義為 public List b,而只用{}嵌套的就定義為 public C c,
具體的大家對照Json字串看看就明白了,不明白的我們可以互相交流,本人也是開發新手!
[java] view plaincopy
- Gson gson = new Gson();
- java.lang.reflect.Type type = new TypeToken() {}.getType();
- JsonBean jsonBean = gson.fromJson(json, type);
然後想拿資料就很簡單啦,直接在jsonBean裡面取就可以了!
如果需要解析的Json嵌套了很多層,同樣可以可以定義一個嵌套很多層內部類的Bean,需要細心的對照Json欄位來定義哦。
下面我將以一個具體的列子來說明通過Gson方式解析複雜的json資料
1.將要解析的資料如下面的格式
{
error: 0,
status: success,
date: 2014-05-10,
results: [
{
currentCity: 南京,
weather_data: [
{
date: 周六(今天, 即時:19℃),
dayPictureUrl: http://api.map.baidu.com/images/weather/day/dayu.png,
nightPictureUrl: http://api.map.baidu.com/images/weather/night/dayu.png,
weather: 大雨,
wind: 東南風5-6級,
temperature: 18℃
},
{
date: 周日,
dayPictureUrl: http://api.map.baidu.com/images/weather/day/zhenyu.png,
nightPictureUrl: http://api.map.baidu.com/images/weather/night/duoyun.png,
weather: 陣雨轉多雲,
wind: 西北風4-5級,
temperature: 21 ~ 14℃
}
]
}
]
}
2.必須定義如下一些的javaBean資料
Status.java
[java] view plaincopy
- public class Status
- {
- private String error;
- private String status;
- private String date;
- private List results;
- public String getError()
- {
- return error;
- }
- public void setError(String error)
- {
- this.error = error;
- }
-
- public String getStatus()
- {
- return status;
- }
- public void setStatus(String status)
- {
- this.status = status;
- }
- public String getDate()
- {
- return date;
- }
- public void setDate(String date)
- {
- this.date = date;
- }
- public List getResults()
- {
- return results;
- }
- public void setResults(List results)
- {
- this.results = results;
- }
- @Override
- public String toString()
- {
- return Status [error= + error + , status= + status
- + , date= + date + , results= + results + ];
- } Results.java
[java] view plaincopy
- public class Results
- {
- private String currentCity;
- private List weather_data;
- public String getCurrentCity()
- {
- return currentCity;
- }
- public void setCurrentCity(String currentCity)
- {
- this.currentCity = currentCity;
- }
- public List getWeather_data()
- {
- return weather_data;
- }
- public void setWeather_data(List weather_data)
- {
- this.weather_data = weather_data;
- }
- @Override
- public String toString()
- {
- return Results [currentCity= + currentCity + , weather_data=
- + weather_data + ];
- }
Weather.java
[java] view plaincopy
- 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) {
- this.date = date;
- }
- public String getDayPictureUrl() {
- return dayPictureUrl;
- }
- public void setDayPictureUrl(String dayPictureUrl) {
- this.dayPictureUrl = dayPictureUrl;
- }
- public String getNightPictureUrl() {
- return nightPictureUrl;
- }
- public void setNightPictureUrl(String nightPictureUrl) {
- this.nightPictureUrl = nightPictureUrl;
- }
- public String getWeather() {
- return weather;
- }
- public void setWeather(String weather) {
- this.weather = weather;
- }
- public String getWind() {
- return wind;
- }
- public void setWind(String wind) {
- this.wind = wind;
- }
- public String getTemperature() {
- return temperature;
- }
- public void setTemperature(String temperature) {
- this.temperature = temperature;
- }
- @Override
- public String toString() {
- return Weather [date= + date + , dayPictureUrl=
- + dayPictureUrl + , nightPictureUrl=
- + nightPictureUrl + , weather= + weather
- + , wind= + wind + , temperature= + temperature
- + ];
- }
- 然後具體的javabean定義好了就將解析資料了,下面就是我的解析資料類
[java] view plaincopy
- public class MainActivity extends Activity
- {
- private Button tojson;
- RequestQueue mQueue;
- StringRequest stringRequest;
- Gson gson;
- String str;
-
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- tojson = (Button)findViewById(R.id.tojson);
- gson = new Gson();
-
- mQueue = Volley.newRequestQueue(MainActivity.this);
- //http://10.19.20.12/upgrade/test.txt是測試使用的json資料
- stringRequest = new StringRequest(http://10.19.20.12/upgrade/test.txt,
- new Response.Listener()
- {
- @Override
- public void onResponse(String response)
- {
- Log.d(TAG, response);
- System.out.println(response=+response);
- Status status = gson.fromJson(response, Status.class);
- System.out.println(status=+status);
- System.out.println(-------------------------------------);
- List result = status.getResults();
- System.out.println(result=+result);
-
- }
- },
- new Response.ErrorListener()
- {
- @Override
- public void onErrorResponse(VolleyError error)
- {
- Log.e(TAG, error.getMessage(), error);
- }
-
- });
-
- tojson.setOnClickListener(new OnClickListener()
- {
- @Override
- public void onClick(View v)
- {
- mQueue.add(stringRequest);
- }
- });
- }
-
-
-
- }
- 其中上面的RequestQueue是開源網路程式庫Volley的使用,如果你對該庫的使用還不熟悉的話可以參考http://blog.csdn.net/guolin_blog/article/details/17482095,該作者對Volley庫的使用講解得非常的細緻和深入
大家可以仔細的去拜讀。 轉載: http://blog.csdn.net/tkwxty/article/details/34474501