编写不易,如有转载,请声明出处:http://blog.csdn.net/zxc514257857/article/details/60490002
前言
本篇博客大致围绕以下几个内容讲解:
- Gson及GsonFormat的基本配置及使用
- Gson解析基本数据类型和POJO类型
- Gson解析Array类型
- 属性重命名 @SerializedName 注解的使用
Gson及GsonFormat的基本配置及使用
使用Gson需要在project的build.gradle下添加:
compile 'com.google.code.gson:gson:2.4'
使用GsonFormat需要通过ctrl+alt+s调处Settings,并找到plugins,选择GsonFormat插件进行安装。如果需要使用JavaBean类,通过alt+insert调出GsonFormat插件使用
Gson解析、生成基本数据类型和POJO类型
- Gson解析基本数据类型
// 解析
Gson gson = new Gson();
int i = gson.fromJson("100", int.class); //100
double d = gson.fromJson("\"99.99\"", double.class); //99.99
boolean b = gson.fromJson("true", boolean.class); // true
String str = gson.fromJson("String", String.class); // String
----------------------------------------------------------------------------------------
// 生成
Gson gson = new Gson();
String jsonInt = gson.toJson(100); // 100
String jsonDouble = gson.toJson(99.99); // 99.99
String jsonBoolean = gson.toJson(false); // false
String jsonString = gson.toJson("String"); //"String"
- Gson解析POJO类型
// User JavaBean类
public class User {
private String name;
private int age;
private String address;
public User(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
'}';
}
}
----------------------------------------------------------------------------------------
// 解析
Gson gson = new Gson();
String str = "{\"age\":24,\"address\":\"杭州\",\"name\":\"zhr\"}";
User user = gson.fromJson(str , User.class);
int age = user .getAge();
String name = user .getName();
String address = user .getAddress();
// age:24,name:zhr,address:杭州
Log.i(TAG , "age:" + age +",name:" + name + ",address:" + address);
----------------------------------------------------------------------------------------
// 生成
Gson gson = new Gson();
User user = new User("zhr" , 24 , "杭州");
String str = gson.toJson(user );
// str:{"address":"杭州","age":24,"name":"zhr"}
Log.i(TAG , "str" + str);
Gson解析Array类型
String str = "[\"00\",\"11\",\"22\",\"33\",\"44\",\"55\",\"66\",\"77\",\"88\",\"99\",\"00\",\"11\",\"22\",\"33\",\"44\",\"55\",\"66\",\"77\",\"88\",\"99\"]";
- Gson解析数组类型
String[] strings = gson.fromJson(str , String[].class);
// strings:[00, 11, 22, 33, 44, 55, 66, 77, 88, 99, 00, 11, 22, 33, 44, 55, 66, 77, 88, 99]
Log.i(TAG , "strings:" + Arrays.toString(strings));
- Gson解析集合类型
List<String> list = gson.fromJson(s3, new TypeToken<List<String>>(){}.getType());
// list:[00, 11, 22, 33, 44, 55, 66, 77, 88, 99, 00, 11, 22, 33, 44, 55, 66, 77, 88, 99]
Log.i(TAG , "list:" + list.toString());
使用gson.fromJson(json , )的时候,集合类型要在bean上包一个list,对象类型直接使用.class即可
属性重命名 @SerializedName 注解的使用
// PriceBean JavaBean类(注意这里通过@SerializedName注解,将is_forsale改成了isForsale)
public class PriceBean {
private String id;
@SerializedName("is_forsale")
private String isForsale;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getIsForsale() {
return isForsale;
}
public void setIsForsale(String isForsale) {
this.isForsale = isForsale;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "PriceBean{" +
"id='" + id + '\'' +
", isForsale='" + isForsale + '\'' +
", name='" + name + '\'' +
'}';
}
}
----------------------------------------------------------------------------------------
// 解析
String jsonArray = "[\n" +
" {\n" +
" \"id\": \"250\",\n" +
" \"is_forsale\": \"0\",\n" +
" \"name\": \"鲢鱼\"\n" +
" },\n" +
" {\n" +
" \"id\": \"629\",\n" +
" \"is_forsale\": \"0\",\n" +
" \"name\": \"鳙鱼\"\n" +
" },\n" +
" {\n" +
" \"id\": \"603\",\n" +
" \"is_forsale\": \"0\",\n" +
" \"name\": \"草鱼\"\n" +
" },\n" +
" {\n" +
" \"id\": \"180\",\n" +
" \"is_forsale\": \"0\",\n" +
" \"name\": \"鲤鱼\"\n" +
" },\n" +
" {\n" +
" \"id\": \"138\",\n" +
" \"is_forsale\": \"0\",\n" +
" \"name\": \"鲫鱼\"\n" +
" },\n" +
" {\n" +
" \"id\": \"631\",\n" +
" \"is_forsale\": \"0\",\n" +
" \"name\": \"河蟹\"\n" +
" },\n" +
" {\n" +
" \"id\": \"685\",\n" +
" \"is_forsale\": \"0\",\n" +
" \"name\": \"冻带鱼\"\n" +
" },\n" +
" {\n" +
" \"id\": \"142\",\n" +
" \"is_forsale\": \"0\",\n" +
" \"name\": \"鲳鱼\"\n" +
" },\n" +
" {\n" +
" \"id\": \"136\",\n" +
" \"is_forsale\": \"0\",\n" +
" \"name\": \"鲈鱼\"\n" +
" },\n" +
" {\n" +
" \"id\": \"181\",\n" +
" \"is_forsale\": \"0\",\n" +
" \"name\": \"大白鱼\"\n" +
" },\n" +
" {\n" +
" \"id\": \"957\",\n" +
" \"is_forsale\": \"0\",\n" +
" \"name\": \"白鲢\"\n" +
" },\n" +
" {\n" +
" \"id\": \"359\",\n" +
" \"is_forsale\": \"0\",\n" +
" \"name\": \"墨鱼\"\n" +
" },\n" +
" {\n" +
" \"id\": \"133\",\n" +
" \"is_forsale\": \"0\",\n" +
" \"name\": \"鳊鱼\"\n" +
" },\n" +
" {\n" +
" \"id\": \"231\",\n" +
" \"is_forsale\": \"0\",\n" +
" \"name\": \"泥鳅\"\n" +
" },\n" +
" {\n" +
" \"id\": \"137\",\n" +
" \"is_forsale\": \"0\",\n" +
" \"name\": \"黄鳝\"\n" +
" },\n" +
" {\n" +
" \"id\": \"131\",\n" +
" \"is_forsale\": \"0\",\n" +
" \"name\": \"黑鱼\"\n" +
" },\n" +
" {\n" +
" \"id\": \"009\",\n" +
" \"is_forsale\": \"0\",\n" +
" \"name\": \"银鱼\"\n" +
" },\n" +
" {\n" +
" \"id\": \"050\",\n" +
" \"is_forsale\": \"0\",\n" +
" \"name\": \"秋刀鱼\"\n" +
" },\n" +
" {\n" +
" \"id\": \"001\",\n" +
" \"is_forsale\": \"0\",\n" +
" \"name\": \"武昌鱼\"\n" +
" },\n" +
" {\n" +
" \"id\": \"100\",\n" +
" \"is_forsale\": \"0\",\n" +
" \"name\": \"鳇鱼\"\n" +
" }\n" +
"]";
List<PriceBean> mList = gson.fromJson(jsonArray, new TypeToken<List<PriceBean>>(){}.getType());
// mList:[PriceBean{id='250', isForsale='0', name='鲢鱼'}, PriceBean{id='629', isForsale='0', name='鳙鱼'}, PriceBean{id='603', isForsale='0', name='草鱼'}, PriceBean{id='180', isForsale='0', name='鲤鱼'}, PriceBean{id='138', isForsale='0', name='鲫鱼'}, PriceBean{id='631', isForsale='0', name='河蟹'}, PriceBean{id='685', isForsale='0', name='带鱼'}, PriceBean{id='142', isForsale='0', name='鲳鱼'}, PriceBean{id='136', isForsale='0', name='鲈鱼'}, PriceBean{id='181', isForsale='0', name='白鱼'}, PriceBean{id='957', isForsale='0', name='白鲢'}, PriceBean{id='359', isForsale='0', name='墨鱼'}, PriceBean{id='133', isForsale='0', name='鳊鱼'}, PriceBean{id='231', isForsale='0', name='泥鳅'}, PriceBean{id='137', isForsale='0', name='黄鳝'}, PriceBean{id='131', isForsale='0', name='黑鱼'}, PriceBean{id='009', isForsale='0', name='银鱼'}, PriceBean{id='050', isForsale='0', name='刀鱼'}, PriceBean{id='001', isForsale='0', name='河豚'}, PriceBean{id='100', isForsale='0', name='鳇鱼'}]
Log.i(TAG , "mList:" + mList);
注意:在PriceBean类中,通过@SerializedName注解,将is_forsale改成了isForsale,这样很好的保留了前端、后台、Android/Php、JS/Java之间各自的命名习惯
----------原文参考自:http://www.jianshu.com/p/e740196225a4----------
----------因本人才疏学浅,如博客或Demo中有错误的地方请大家随意指出,与大家一起讨论,共同进步,谢谢!----------