forked from aofeng/JavaTutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArraySerialize.java
More file actions
41 lines (33 loc) · 1000 Bytes
/
ArraySerialize.java
File metadata and controls
41 lines (33 loc) · 1000 Bytes
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
package cn.aofeng.demo.json.gson;
import com.google.gson.Gson;
/**
* 数组的序列化。
*
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
*/
public class ArraySerialize {
public void serialize(Object[] arr) {
Gson gson = new Gson();
System.out.println( gson.toJson(arr) );
}
public static void main(String[] args) {
ArraySerialize as = new ArraySerialize();
// 整型对象数组
Integer[] intArr = new Integer[3];
intArr[0] = 9;
intArr[1] = 7;
intArr[2] = 5;
as.serialize(intArr);
// 字符串数组
String[] names = new String[3];
names[0] = "张三";
names[1] = "李四";
names[2] = "王五";
as.serialize(names);
// 对象数组
Person[] persons = new Person[2];
persons[0] = new Person("小明", 10);
persons[1] = new Person("马丽", 9);
as.serialize(persons);
}
}