摘要:Gson解析复杂的json数据 在这里介绍解析json数据的另外一种方法就是通过Gson解析,对于解析比较简单的json数据我就不介绍了来一个比较复杂一点的json数据,如下面我们要解析的一个json数据: [java]view plaincopy ...

Gson解析复杂的json数据

在这里介绍解析json数据的另外一种方法就是通过Gson解析,对于解析比较简单的json数据我就不介绍了来一个比较复杂一点的json数据,如下面我们要解析的一个json数据:

[java]view plaincopy
  1. Stringjson={"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]view plaincopy
  1. publicclassJsonBean{
  2. publicStringa;
  3. publicList<B>b;
  4. publicCc;
  5. publicstaticclassB{
  6. publicStringb1;
  7. publicStringb2;
  8. }
  9. publicstaticclassC{
  10. publicStringc1;
  11. publicStringc2;
  12. }
  13. }

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

[java]view plaincopy
  1. Gsongson=newGson();
  2. java.lang.reflect.Typetype=newTypeToken<JsonBean>(){}.getType();
  3. JsonBeanjsonBean=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
  1. publicclassStatus
  2. {
  3. privateStringerror;
  4. privateStringstatus;
  5. privateStringdate;
  6. privateList<Results>results;
  7. publicStringgetError()
  8. {
  9. returnerror;
  10. }
  11. publicvoidsetError(Stringerror)
  12. {
  13. this.error=error;
  14. }
  15. publicStringgetStatus()
  16. {
  17. returnstatus;
  18. }
  19. publicvoidsetStatus(Stringstatus)
  20. {
  21. this.status=status;
  22. }
  23. publicStringgetDate()
  24. {
  25. returndate;
  26. }
  27. publicvoidsetDate(Stringdate)
  28. {
  29. this.date=date;
  30. }
  31. publicList<Results>getResults()
  32. {
  33. returnresults;
  34. }
  35. publicvoidsetResults(List<Results>results)
  36. {
  37. this.results=results;
  38. }
  39. @Override
  40. publicStringtoString()
  41. {
  42. return"Status[error="+error+",status="+status
  43. +",date="+date+",results="+results+"]";
  44. }
Results.java
[java]view plaincopy
  1. publicclassResults
  2. {
  3. privateStringcurrentCity;
  4. privateList<Weather>weather_data;
  5. publicStringgetCurrentCity()
  6. {
  7. returncurrentCity;
  8. }
  9. publicvoidsetCurrentCity(StringcurrentCity)
  10. {
  11. this.currentCity=currentCity;
  12. }
  13. publicList<Weather>getWeather_data()
  14. {
  15. returnweather_data;
  16. }
  17. publicvoidsetWeather_data(List<Weather>weather_data)
  18. {
  19. this.weather_data=weather_data;
  20. }
  21. @Override
  22. publicStringtoString()
  23. {
  24. return"Results[currentCity="+currentCity+",weather_data="
  25. +weather_data+"]";
  26. }

Weather.java
[java]view plaincopy
  1. publicclassWeather{
  2. privateStringdate;
  3. privateStringdayPictureUrl;
  4. privateStringnightPictureUrl;
  5. privateStringweather;
  6. privateStringwind;
  7. privateStringtemperature;
  8. publicStringgetDate(){
  9. returndate;
  10. }
  11. publicvoidsetDate(Stringdate){
  12. this.date=date;
  13. }
  14. publicStringgetDayPictureUrl(){
  15. returndayPictureUrl;
  16. }
  17. publicvoidsetDayPictureUrl(StringdayPictureUrl){
  18. this.dayPictureUrl=dayPictureUrl;
  19. }
  20. publicStringgetNightPictureUrl(){
  21. returnnightPictureUrl;
  22. }
  23. publicvoidsetNightPictureUrl(StringnightPictureUrl){
  24. this.nightPictureUrl=nightPictureUrl;
  25. }
  26. publicStringgetWeather(){
  27. returnweather;
  28. }
  29. publicvoidsetWeather(Stringweather){
  30. this.weather=weather;
  31. }
  32. publicStringgetWind(){
  33. returnwind;
  34. }
  35. publicvoidsetWind(Stringwind){
  36. this.wind=wind;
  37. }
  38. publicStringgetTemperature(){
  39. returntemperature;
  40. }
  41. publicvoidsetTemperature(Stringtemperature){
  42. this.temperature=temperature;
  43. }
  44. @Override
  45. publicStringtoString(){
  46. return"Weather[date="+date+",dayPictureUrl="
  47. +dayPictureUrl+",nightPictureUrl="
  48. +nightPictureUrl+",weather="+weather
  49. +",wind="+wind+",temperature="+temperature
  50. +"]";
  51. }
然后具体的javabean定义好了就将解析数据了,下面就是我的解析数据类
[java]view plaincopy
  1. publicclassMainActivityextendsActivity
  2. {
  3. privateButtontojson;
  4. RequestQueuemQueue;
  5. StringRequeststringRequest;
  6. Gsongson;
  7. Stringstr;
  8. @Override
  9. protectedvoidonCreate(BundlesavedInstanceState)
  10. {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_main);
  13. tojson=(Button)findViewById(R.id.tojson);
  14. gson=newGson();
  15. mQueue=Volley.newRequestQueue(MainActivity.this);
  16. //http://10.19.20.12/upgrade/test.txt是测试使用的json数据
  17. stringRequest=newStringRequest("http://10.19.20.12/upgrade/test.txt",
  18. newResponse.Listener<String>()
  19. {
  20. @Override
  21. publicvoidonResponse(Stringresponse)
  22. {
  23. Log.d("TAG",response);
  24. System.out.println("response="+response);
  25. Statusstatus=gson.fromJson(response,Status.class);
  26. System.out.println("status="+status);
  27. System.out.println("-------------------------------------");
  28. List<Results>result=status.getResults();
  29. System.out.println("result="+result);
  30. }
  31. },
  32. newResponse.ErrorListener()
  33. {
  34. @Override
  35. publicvoidonErrorResponse(VolleyErrorerror)
  36. {
  37. Log.e("TAG",error.getMessage(),error);
  38. }
  39. });
  40. tojson.setOnClickListener(newOnClickListener()
  41. {
  42. @Override
  43. publicvoidonClick(Viewv)
  44. {
  45. mQueue.add(stringRequest);
  46. }
  47. });
  48. }
  49. }
  50. </span>
其中上面的RequestQueue是开源网络库Volley的使用,如果你对该库的使用还不熟悉的话可以参考http://blog.csdn.net/guolin_blog/article/details/17482095,该作者对Volley库的使用讲解得非常的细致和深入
大家可以仔细的去拜读。
转载:http://blog.csdn.net/tkwxty/article/details/34474501

更多相关文章

  1. 无法从Android中的Asset文件夹复制数据库
  2. Android-Dialog对话框 全解(普通对话框,单选对话框,多选对话框,列表
  3. 关于activity之间及activity与baseAdapter,activity与Fragment的
  4. Android自定义Toast带图片的
  5. Android 在资源文件(res/strings.xml)定义一维数组,间接定义二维数
  6. Android防止活动被回收而丢失数据
  7. Android之SQLite数据库篇
  8. Android 自定义控件 改变图片颜色来实现类似selector点击更改颜
  9. Eclipse Java:根据构建配置定义最终变量

随机推荐

  1. AndroidStudio使用教程(第一弹)
  2. Cocos2d-x C++调用Android弹出提示框
  3. Android 麦克风录音动画
  4. json解析android客户端源码
  5. 不错的Android开发资料,收藏一下
  6. android视频录制MediaStore.ACTION_VIDEO
  7. android:debuggable属性
  8. widget(1、TextView)
  9. Android进度条源代码
  10. Dialog窗口形式的Activity