一. 什么是 JSON 数据类型

1. 在 JSON 中,值必须是以下数据类型之一

  • 字符串
  • 数字
  • 对象(JSON对象)
  • 数组
  • 布尔
  • null

2. 在 JSON 中,值不可以是以下数据类型之一

  • 函数
  • 日期
  • undefined

3. JSON 的具体数据类型

① JSON字符串:JSON 中的字符串必须用双引号包围。

1
{ "name":"John" }

② JSON数字:JSON 中的数字必须是整数或浮点数。

1
{ "age":30 }

③ JSON对象:对象一定使用 { } 包着的。

1
2
3
{
"employee":{ "name":"Bill Gates", "age":62, "city":"Seattle" }
}

④ JSON数组:数组一定使用 [ ] 包着的。

1
2
3
{
"employees":[ "Bill", "Steve", "David" ]
}

⑤ JSON布尔:JSON 中的值可以是 true/false。

1
{ "sale":true }

⑥ JSON null:JSON 中的值可以是 null。

1
{ "middlename":null }

二. 解析JSON数据

1. 原生解析JSON

① 系统提供的关于JSON的类

② 实际例子

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* 原生:解析JSON数据
*/
private void testJSONParse(){
//获取json字符串
InputStream is = getResources().openRawResource(R.raw.json);
String jsonString = getStringByInputStream(is);

try {
//获取json对象
JSONObject jsonObject = new JSONObject(jsonString);

//解析json对象
User user = new User();
JsonModel jsonModel = new JsonModel();
{
//获取其中的json对象
JSONObject user_json = jsonObject.getJSONObject("user");
user.id = user_json.getLong("id");
user.name = user_json.getString("name");
user.avatar = user_json.getString("avatar");
jsonModel.user = user;

//获取其中的字符串
jsonModel.title = jsonObject.getString("title");
jsonModel.content = jsonObject.getString("content");

//获取其中的数组
JSONArray images = jsonObject.getJSONArray("images");
for (int i = 0; i < images.length(); i++) {
jsonModel.images.add((String) images.get(i));
}

//获取其中的字符串
jsonModel.block = jsonObject.getString("block");
jsonModel.discussNumber = jsonObject.getString("discussNumber");
jsonModel.datetime = jsonObject.getString("datetime");
}

//测试
System.out.println(jsonModel);
} catch (JSONException e) {
e.printStackTrace();
}
}

/**
* 将输入流->JSON字符串
* @param is
* @return
*/
private String getStringByInputStream(InputStream is) {
String result;
ByteArrayOutputStream bos = new ByteArrayOutputStream();

byte[] data = new byte[1024];
int len = 0;

try {
//一直读取
while ((len = is.read(data)) != -1){
bos.write(data,0,len);
}
} catch (IOException e) {
e.printStackTrace();
}

result = new String(bos.toByteArray());

return result;
}
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
//手写的JSON数据模型
public class JsonModel {
public User user;
public String title;
public String content;
public List<String> images;
public String block;
public String discussNumber;
public String datetime;

public JsonModel() {
images = new ArrayList<>();
}

@Override
public String toString() {
return "JsonModel{" +
"user=" + user +
", title='" + title + '\'' +
", content='" + content + '\'' +
", images=" + images +
", block='" + block + '\'' +
", discussNumber='" + discussNumber + '\'' +
", datetime='" + datetime + '\'' +
'}';
}
}

③ 运行效果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
JsonModel{
user=User{
id=1000001,
name='年少醉娇娆',
avatar='http://tp3.sinaimg.cn/5620612798/180/5733537191/0'
},

title='请大家帮我掌掌眼~', content='翻箱倒柜,找到40多枚民国铜币,请鉴赏下。',

images=[
http://ww2.sinaimg.cn/bmiddle/8675fb02gw1exy6fh4lulj20p018gq7m.jpg,
http://ww3.sinaimg.cn/bmiddle/8675fb02gw1exy6fhrl3vj20p018g78l.jpg,
http://ww2.sinaimg.cn/bmiddle/8675fb02gw1exy6findz0j20p018gn1t.jpg
],

block='古玩鉴赏',
discussNumber='101',
datetime='2015-12-10 09:40:02'
}

2. 第三方库 GSON 解析

① 使用 AndroidStudio 插件 GsonFormat 生成 JSON数据模型类

GsonFormat插件快捷调用:option + S。

使用步骤:

  • 创建一个空的类,复制一条JSON数据。

  • 按住 option + S,在弹出的表格框中填入复制的JSON数据,OK即可。

注意:

如果生成了内容类,会自动加上static标识。

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
public class Bean {

/**
* user : {"id":1000001,"name":"年少醉娇娆","avatar":"http://tp3.sinaimg.cn/5620612798/180/5733537191/0"}
* title : 请大家帮我掌掌眼~
* content : 翻箱倒柜,找到40多枚民国铜币,请鉴赏下。
* images : ["http://ww2.sinaimg.cn/bmiddle/8675fb02gw1exy6fh4lulj20p018gq7m.jpg","http://ww3.sinaimg.cn/bmiddle/8675fb02gw1exy6fhrl3vj20p018g78l.jpg","http://ww2.sinaimg.cn/bmiddle/8675fb02gw1exy6findz0j20p018gn1t.jpg"]
* block : 古玩鉴赏
* discussNumber : 101
* datetime : 2015-12-10 09:40:02
*/

private UserBean user;
private String title;
private String content;
private String block;
private String discussNumber;
private String datetime;
private List<String> images;

public UserBean getUser() {
return user;
}

public void setUser(UserBean user) {
this.user = user;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

public String getBlock() {
return block;
}

public void setBlock(String block) {
this.block = block;
}

public String getDiscussNumber() {
return discussNumber;
}

public void setDiscussNumber(String discussNumber) {
this.discussNumber = discussNumber;
}

public String getDatetime() {
return datetime;
}

public void setDatetime(String datetime) {
this.datetime = datetime;
}

public List<String> getImages() {
return images;
}

public void setImages(List<String> images) {
this.images = images;
}

@Override
public String toString() {
return "Bean{" +
"user=" + user +
", title='" + title + '\'' +
", content='" + content + '\'' +
", block='" + block + '\'' +
", discussNumber='" + discussNumber + '\'' +
", datetime='" + datetime + '\'' +
", images=" + images +
'}';
}

public static class UserBean {
/**
* id : 1000001
* name : 年少醉娇娆
* avatar : http://tp3.sinaimg.cn/5620612798/180/5733537191/0
*/

private int id;
private String name;
private String avatar;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAvatar() {
return avatar;
}

public void setAvatar(String avatar) {
this.avatar = avatar;
}

@Override
public String toString() {
return "UserBean{" +
"id=" + id +
", name='" + name + '\'' +
", avatar='" + avatar + '\'' +
'}';
}
}
}

② 使用第三方库 GSON

第三方库 GSON 的地址:GSON

简单的使用:

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
/**
* 第三方GSON库:解析JSON数据
*/
private void testJSONParseByGSON(){
//获取json字符串
InputStream is = getResources().openRawResource(R.raw.json);
String jsonString = getStringByInputStream(is);

//使用第三方库解析
Gson gson = new Gson();
Bean bean = gson.fromJson(jsonString, Bean.class);

//测试
System.out.println(bean);
}

/**
* 将输入流->JSON字符串
* @param is
* @return
*/
private String getStringByInputStream(InputStream is) {
String result;
ByteArrayOutputStream bos = new ByteArrayOutputStream();

byte[] data = new byte[1024];
int len = 0;

try {
//一直读取
while ((len = is.read(data)) != -1){
bos.write(data,0,len);
}
} catch (IOException e) {
e.printStackTrace();
}

result = new String(bos.toByteArray());

return result;
}

③ 运行结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Bean{
user=UserBean{
id=1000001,
name='年少醉娇娆',
avatar='http://tp3.sinaimg.cn/5620612798/180/5733537191/0'
},

title='请大家帮我掌掌眼~',
content='翻箱倒柜,找到40多枚民国铜币,请鉴赏下。',
block='古玩鉴赏',
discussNumber='101',
datetime='2015-12-10 09:40:02',

images=[
http://ww2.sinaimg.cn/bmiddle/8675fb02gw1exy6fh4lulj20p018gq7m.jpg,
http://ww3.sinaimg.cn/bmiddle/8675fb02gw1exy6fhrl3vj20p018g78l.jpg,
http://ww2.sinaimg.cn/bmiddle/8675fb02gw1exy6findz0j20p018gn1t.jpg
]
}

三. 源码地址

ParseJson

四. Fastjson

1. 简介

Fastjson 是一个 Java 库,可以将 Java 对象转换为 JSON 格式,当然它也可以将 JSON 字符串转换为 Java 对象。

2. Github地址

FastJson

3. FastJson的简明教程

Fastjson 简明教程

参考文章

解析JSON数据的详细步骤以及偷懒技巧(使用第三方库GSON以及GsonFormat工具)