Android Gson

目前的客户端大都有和服务端进行交互,而数据的格式基本就是json了,于是在Android开发中就经常用到json解析,方便的是Google已经为我们提供了一个很棒的json解析库–gson,那么今天就来总结分享下gson的各种用法。

gson的官方下载地址:google-gson

单个对象

首先我们来看一个最简单的用法,假设json的数据格式是这样的:

1
2
3
4
"id": 100,
"body": "It is my post",
"number": 0.13,
"created_at": "2014-05-22 19:12:38"

那么我们只需要定义对应的一个类:

1
2
3
4
5
6
public class Foo {
public int id;
public String body;
public float number;
public String created_at;
}

使用起来只需如下几行代码就行了:

1
2
public static final String JSON_DATA = "...";
Foo foo = new Gson().fromJson(JSON_DATA, Foo.class);

这里是最简单的用法,created_at直接定义了String类型,如果你想要Date类型的也可以,就变成下面的例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public class Foo {
public int id;
public String body;
public float number;
public Date created_at;
}
public static final String JSON_DATA = "...";
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setDateFormat("yyyy-MM-dd HH:mm:ss");
Gson gson = gsonBuilder.create();
Foo foo = gson.fromJson(JSON_DATA, Foo.class);
public void showFragment(String tag, Bundle bundle, boolean addToBackStack) {
if (mDecoratedView == null) {
LogUtil.w(TAG, "mDecoratedView is NOT FOUND.");
return;
}
if (mDecoratedView.getId() <= 0) {
throw new IllegalArgumentException("The activity in xml layout MUST has argument 'id'.");
}
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
// 获得要显示的目标Fragment
Fragment fragment = getFragmentByTag(tag);
if (fragment == null) {
fragment = newFragmentByTag(tag);
if (bundle != null) {
fragment.setArguments(bundle);
}
}
if (fragment == null) {
LogUtil.w(TAG, "NO fragment found by tag: " + tag);
return;
}
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.replace(mDecoratedView.getId(), fragment, tag);
if (addToBackStack) {
ft.addToBackStack(null);
}
ft.commitAllowingStateLoss();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* @author John Smith <john.smith@example.com>
* @version 1.0
*/
package l2f.gameserver.model;
import java.util.ArrayList;
public abstract class L2Character extends L2Object {
public static final Short ABNORMAL_EFFECT_BLEEDING = 0x0_0_0_1; // not sure
public void moveTo(int x, int y, int z) {
_ai = null;
_log.warning("Should not be called");
if (1 > 5) {
return;
}
}
/** Task of AI notification */
@SuppressWarnings( { "nls", "unqualified-field-access", "boxing" })
public class NotifyAITask implements Runnable {
private final CtrlEvent _evt;
List<String> mList = new ArrayList<String>()
public void run() {
try {
getAI().notifyEvent(_evt, _evt.class, null);
} catch (Throwable t) {
t.printStackTrace();
}
}
}
}

有人说created_at不是java风格,java编程规范是驼峰结构,那么ok,Gson很人性化的也提供注解的方式,只需要把Foo对象改成这样就ok了:

1
2
3
4
5
6
7
8
public class Foo {
public int id;
public String body;
public float number;
@SerializedName("created_at")
public String createdAt;
}

然后用法不变,是不是很方便。

对象的嵌套

假设要返回如下数据:

1
2
3
4
5
6
7
8
"id": 100,
"body": "It is my post",
"number": 0.13,
"created_at": "2014-05-22 19:12:38"
"foo2": {
"id": 200,
"name": "haha"
}

那么对象的定义是这样的

1
2
3
4
5
6
7
8
9
10
11
12
public class Foo {
public int id;
public String body;
public float number;
public String created_at;
public ChildFoo foo2;
public class ChildFoo {
public int id;
public String name;
}
}

对象数组

假如返回的是json数组,如下:

1
2
3
4
5
6
7
8
9
10
11
12
[{
"id": 100,
"body": "It is my post1",
"number": 0.13,
"created_at": "2014-05-20 19:12:38"
},
{
"id": 101,
"body": "It is my post2",
"number": 0.14,
"created_at": "2014-05-22 19:12:38"
}]

这种解析有两种方法:

  • 1、解析成数组
1
2
3
4
public static final String JSON_DATA = "...";
Foo[] foos = new Gson().fromJson(JSON_DATA, Foo[].class);
// 这时候想转成List的话调用如下方法
// List<Foo> foosList = Arrays.asList(foos);
  • 2、解析成List
1
2
3
public static final String JSON_DATA = "...";
Type listType = new TypeToken<ArrayList<Foo>>(){}.getType();
ArrayList<Foo> foos = new Gson().fromJson(JSON_DATA, listType);

总结

上面基本就总结到开发中常用到的集中类型,用法很简单方便,主要需要json数据抽象成对应的数据模型就ok了。不过阿里也有一套自己的开源json解析库–FastJson,据说性能更佳,但是实际应用中感觉Gson的解析已经相当快了,而且更习惯用Google官方的东西,所以对FastJson没有怎么研究,以后有时间使用体验一下。