安卓Gson对json数据的解析

时间:2021-02-04 22:34:28

格式一

数据:

{
"status":1,
"error_code":0,
"error_desc":"数据不存在",
"result":"data"
}


Bean文件:

private String status;

private String error_code;

private String error_desc;

private String result;


数据获取方式:

Gson  mGson = new Gson();

Bean文件 bean名称 = mGson.fromJson(json字符串,Bean文件.class);

直接通过get的方式进行获取,例如:dataMessage.getError_desc();


格式二

数据:

{
"status":1,
"error_code":0,
"error_desc":"数据不存在",
"result":{
"name":"pony",
"age":26,
"images":[
"http://qiniucdn.dailyyoga.com.cn/47/4b/474b491ecd6ccf72178784ad9bdb90b9.jpeg",
"http://qiniucdn.dailyyoga.com.cn/05/1a/051a09c9dae7fe3347184940b06b83c3.jpeg",
"http://qiniucdn.dailyyoga.com.cn/24/7a/247a944135495931ab7c48475009ae61.jpeg"
]
}
}

Bean文件:


 Bean文件第一层:

private String status;

private String error_code;

private String error_desc;

private ResultBeanresult;

因为ResultBean 是一个bean文件,所以要对它在创建一个bean文件;


 Bean文件第二层:                                                      

 private String name;

  private int age;

private String[] images;

因为images字段在json数据里面是数组的文件,所以在这边解析的时候,数据类型要对应

数据获取方式:

GsonmGson = new Gson();

Bean文件 bean名称 = mGson.fromJson(json字符串,Bean文件.class);

(1):第一层的获取;

 bean.get字段名称;

(2):第二层的获取

字段的获取:dataMessage.getResult().getName()    

 图片数组的获取:dataMessage.getResult().getImages()[0],取得数据的第一个;


格式三

数据:

{
"status":1,
"error_code":0,
"error_desc":"数据不存在",
"result":{
"name":"pony",
"age":26,
"hottopic":[
{
"username":"世间修行",
"userId":111452,
"gender":1,
"isVip":0,
"auth":1
},
{
"username":"世间修行",
"userId":111452,
"gender":1,
"isVip":0,
"auth":1
}
]
}
}

Bean文件:

Bean第一层:

private String status;

 private String error_code;

 private String error_desc;

private ResultBeanresult;


Bean第二层:

private String name;

private int age;

private List<HottopicBean>hottopic;


Bean第三层

private Stringusername;

private int userId;

private int gender;

private int isVip;

private int auth;;


数据获取方式:

 GsonmGson = new Gson();

Bean文件 bean名称 = mGson.fromJson(json字符串,Bean文件.class);

 Get进行数据的获取


格式四

数据:

{
"status":1,
"error_code":0,
"error_desc":"数据不存在",
"result":[
{
"username":"世间修行",
"userId":111452,
"gender":1,
"isVip":0,
"auth":1
},
{
"username":"世间修行",
"userId":111452,
"gender":1,
"isVip":0,
"auth":1
}
]
}

Bean文件:

Bean第一层:

private String status;

private String error_code;

private String error_desc;

private List<HottopicBean>result;


Bean第二层:

private String username;

private int userId;

private int gender;

private int isVip;

private int auth;

 

数据获取方式:

GsonmGson = new Gson();

 Bean文件 bean名称 = mGson.fromJson(json字符串,Bean文件.class);

 Get进行数据的获取


格式五

数据:

{
"user":[
{
"nickname":"Zlatan",
"uid":90425120,
"is_vip":1
}
],
"person":[
{
"name":"Zlatan",
"age":26
}
],
"goods":[
{
"goodsName":"水果",
"goodsMoney":20
}
]

}

Bean文件:


Bean第一层:

 privateList<FiveUserBean> user;

 privateList<FivePersonBean> person;

privateList<FiveGoodsBean> goods;


Bean第二层的第一类:

private Stringnickname;

 private int uid;

private int is_vip;


Bean第二层的第二类:

private String name;

private int age;


Bean第二层的第三类:

private String goodsName;

private intgoodsMoney;


数据获取方式:

 GsonmGson = new Gson();

Bean文件 bean名称 = mGson.fromJson(json字符串,Bean文件.class);

Get进行数据的获取

格式六

数据:

[
{
"name":"Zlatan",
"age":26
},
{
"name":"Zlatan",
"age":27
},
{
"name":"Zlatan",
"age":28
}
]

Bean文件:

private String name;

private int age;

数据获取方式:

 List<Bean文件> 集合名称 = mGson.fromJson(json字符串, new TypeToken<List<Bean文件>>() {}.getType());      

Get进行数据的获取