JSON解析

1,JSON简介

概念

JSON的全称是JavaScript Object Notation,是一种轻量级的数据交换格式。

特点

  1. 本质就是具有特定格式的字符串
  2. JSON完全独立于编程语言
  3. JSON比XML数据传输的有效性要高出很多

2,JSON数据格式

  1. 整体结构 String json1 = “{“id”: 12, “name”: “Tom”}”;

    ​ String json2 = “[{“id”: 12, “name”: “Tom”}, {“id”: 13, “name”: “Danan”}]”

  2. Json对象: {}

  3. Json数组: []

3,JSON解析方向

  1. 将Java对象转换为json格式字符串 在服务器端应用
  2. 将json格式转换为Java对象 在客户端应用
  3. json和Java之间的转换关系 json对应Java对象 json数组对应Java对象的list

4,JSON解析技术

1,Android原生技术

特点

编程相对复杂

数据之间转换实例

1,将json格式的字符串{}转换为java对象
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
//这是最核心的代码
private void jsonToJavaObject() {
String json = "{\n" +
" \"code\": 200,\n" +
" \"message\": \"成功!\",\n" +
" \"id\": 676,\n" +
" \"time\": \"2020-02-16 04:00:00\",\n" +
" \"img\": \"https://img.lijinshan.site/images/300f3da4f8644deb8ae453bc63bdff7c\"\n" +
"}";

ImageInfo imageInfo = null;
try {
JSONObject jsonObject = new JSONObject(json);
//optString 和 getString的区别就是 optString在key值不存在的情况下会返回一个空字符串或者指定的默认值,getString方法会出现空指针异常的错误
int code = jsonObject.optInt("code");
int id = jsonObject.getInt("code");
String message = jsonObject.optString("message");
String message = jsonObject.getString("message");
String time = jsonObject.getString("time");
String img = jsonObject.getString("img");
imageInfo = new ImageInfo(code, message, id, time, img);
} catch (JSONException e) {
e.printStackTrace();
}

native_orignal.setText(json);

native_last.setText(imageInfo.toString());
}
2,将json格式的字符串[]转换为java对象的List
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
 //最核心的代码
private void jsonToJavaList() {
String json = "[\n" +
" {\n" +
" \"id\": 680,\n" +
" \"time\": \"2020-02-16 04:00:00\",\n" +
" \"img\": \"https://img.lijinshan.site/images/65a4da36c01b4835b2327c47523d2a2c\"\n" +
" },\n" +
" {\n" +
" \"id\": 681,\n" +
" \"time\": \"2020-02-16 04:00:00\",\n" +
" \"img\": \"https://img.lijinshan.site/images/c9e4d064cfe340fea3f41e2ef5034596\"\n" +
" },\n" +
" ]";

ArrayList<ImageListInfo> arrayList = new ArrayList<>();
try {
JSONArray jsonArray = new JSONArray(json);
for (int i = 0; i < jsonArray.length(); i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
if(jsonObject != null){
int id = jsonObject.getInt("id");
String time = jsonObject.getString("time");
String img = jsonObject.getString("img");
ImageListInfo imageListInfo = new ImageListInfo(id, img, time);
arrayList.add(imageListInfo);
}

}

} catch (JSONException e) {
e.printStackTrace();
}

native_orignal.setText(json);
native_last.setText(arrayList.toString());
}
3,复杂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
 //最核心的代码
private void jsonToComplex() {
String json = "{\n" +
" \"code\": 200,\n" +
" \"message\": \"成功!\",\n" +
" \"result\": [\n" +
" {\n" +
" \"id\": 680,\n" +
" \"time\": \"2020-02-16 04:00:00\",\n" +
" \"img\": \"https://img.lijinshan.site/images/65a4da36c01b4835b2327c47523d2a2c\"\n" +
" },\n" +
" {\n" +
" \"id\": 681,\n" +
" \"time\": \"2020-02-16 04:00:00\",\n" +
" \"img\": \"https://img.lijinshan.site/images/c9e4d064cfe340fea3f41e2ef5034596\"\n" +
" },\n" +
" {\n" +
" \"id\": 682,\n" +
" \"time\": \"2020-02-16 04:00:00\",\n" +
" \"img\": \"https://img.lijinshan.site/images/8733844e0b954e7e8e29102cefa32dbf\"\n" +
" }\n" +
" ]\n" +
"}";

//封装Java对象
ImageComplexInfo imageComplexInfo = new ImageComplexInfo();

try {
JSONObject jsonObject = new JSONObject(json);

//第一层解析
JSONArray result = jsonObject.optJSONArray("result");
String message = jsonObject.optString("message");
int code = jsonObject.optInt("code");
//第一层封装
imageComplexInfo.setMessage(message);
imageComplexInfo.setCode(code);

List<ImageComplexInfo.ResultBean> resultBean = new ArrayList<>();
imageComplexInfo.setResult(resultBean);

//第二层解析
for (int i = 0; i < result.length(); i++){
JSONObject jsonObject1 = result.optJSONObject(i);
if(jsonObject1 != null){
int id = jsonObject1.getInt("id");
String time = jsonObject1.getString("time");
String img = jsonObject1.getString("img");
ImageComplexInfo.ResultBean bean = new ImageComplexInfo.ResultBean();
bean.setId(id);
bean.setImg(img);
bean.setTime(time);
resultBean.add(bean);
}
}
} catch (JSONException e) {
e.printStackTrace();
}

native_orignal.setText(json);
native_last.setText(imageComplexInfo.toString());

}

2,Gson框架技术

特点

编码简洁,谷歌官方推荐

数据之间的转换实例

1,将json格式的字符串{}转换为Java对象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private void jsonToJavaObjByGson() {
String json = "{\n" +
" \"code\": 200,\n" +
" \"message\": \"成功!\",\n" +
" \"id\": 676,\n" +
" \"time\": \"2020-02-16 04:00:00\",\n" +
" \"img\": \"https://img.lijinshan.site/images/300f3da4f8644deb8ae453bc63bdff7c\"\n" +
"}";

//核心代码
Gson gson = new Gson();
ImageInfo imageInfo = gson.fromJson(json, ImageInfo.class);

gson_native_orignal.setText(json);
gson_native_last.setText(imageInfo.toString());
}
2,将json格式的字符串[]转换为Java对象的List
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
private void jsontoJavaListByGson() {
String json = "[\n" +
" {\n" +
" \"id\": 680,\n" +
" \"time\": \"2020-02-16 04:00:00\",\n" +
" \"img\": \"https://img.lijinshan.site/images/65a4da36c01b4835b2327c47523d2a2c\"\n" +
" },\n" +
" {\n" +
" \"id\": 681,\n" +
" \"time\": \"2020-02-16 04:00:00\",\n" +
" \"img\": \"https://img.lijinshan.site/images/c9e4d064cfe340fea3f41e2ef5034596\"\n" +
" },\n" +
" ]";

//重点核心
Gson gson = new Gson();
List<ImageListInfo> imageListInfo = gson.fromJson(json, new TypeToken<List<ImageListInfo>>(){}.getType());

gson_native_orignal.setText(json);
gson_native_last.setText(imageListInfo.toString());
}
3,将Java对象转换为json字符串{}
1
2
3
4
5
6
7
8
private void javaToJsonByGson() {
ImageInfo imageInfo = new ImageInfo(200, "成功", 23, "2021/8/10", "http://www.imaoxiantuan.com");
Gson gson = new Gson();
String json = gson.toJson(imageInfo);

gson_native_orignal.setText(imageInfo.toString());
gson_native_last.setText(json);
}
4,将Java对象的List转换为json字符串[]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
private void javaToJsonArrayByJson() {
List<ImageListInfo> images = new ArrayList<>();
ImageListInfo oneImage = new ImageListInfo(23, "http://www.id,com", "2021/8/9");
ImageListInfo secondImage = new ImageListInfo(25, "http://www.id.com.cn", "2021/8/10");
images.add(oneImage);
images.add(secondImage);

Gson gson = new Gson();
String json = gson.toJson(images);

gson_native_orignal.setText(images.toString());
gson_native_last.setText(json);

}

3,FastJson框架技术

特点

FastJson是一个Java语言编写的高性能且功能完善的JSON库,它采用的是一种“假定有序快速匹配”的算法,把JSONParse的性能提升到了极致,是目前Java语言中最快的JSON库

数据之间的转换实例

1,将json格式的字符串{}转换为Java对象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
private void jsonToJavaObjByFastjson() {
String json = "{\n" +
" \"code\": 200,\n" +
" \"message\": \"成功!\",\n" +
" \"id\": 676,\n" +
" \"time\": \"2020-02-16 04:00:00\",\n" +
" \"img\": \"https://img.lijinshan.site/images/300f3da4f8644deb8ae453bc63bdff7c\"\n" +
"}";
ImageInfo imageInfo = JSON.parseObject(json, ImageInfo.class);

fastjson_native_orignal.setText(json);
fastjson_native_last.setText(imageInfo.toString());

}
2,将json格式的字符串[]转换为Java对象的List
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
private void jsontoJavaListByFastjson() {
String json = "[\n" +
" {\n" +
" \"id\": 680,\n" +
" \"time\": \"2020-02-16 04:00:00\",\n" +
" \"img\": \"https://img.lijinshan.site/images/65a4da36c01b4835b2327c47523d2a2c\"\n" +
" },\n" +
" {\n" +
" \"id\": 681,\n" +
" \"time\": \"2020-02-16 04:00:00\",\n" +
" \"img\": \"https://img.lijinshan.site/images/c9e4d064cfe340fea3f41e2ef5034596\"\n" +
" },\n" +
" ]";
List<ImageListInfo> images = JSON.parseArray(json, ImageListInfo.class);
fastjson_native_orignal.setText(json);
fastjson_native_last.setText(images.toString());
}
3,将Java对象转换为json字符串{}
1
2
3
4
5
6
7
private void javaToJsonByFastjson() {
ImageInfo imageInfo = new ImageInfo(200, "成功", 23, "2021/8/10", "http://www.imaoxiantuan.com");
String json = JSON.toJSONString(imageInfo);

fastjson_native_orignal.setText(imageInfo.toString());
fastjson_native_last.setText(json);
}
4,将Java对象的List转换为json字符串[]
1
2
3
4
5
6
7
8
9
10
11
private void javaToJsonArrayByFastjson() {
List<ImageListInfo> images = new ArrayList<>();
ImageListInfo oneImage = new ImageListInfo(23, "http://www.id,com", "2021/8/9");
ImageListInfo secondImage = new ImageListInfo(25, "http://www.id.com.cn", "2021/8/10");
images.add(oneImage);
images.add(secondImage);
String json = JSON.toJSONString(images);

fastjson_native_orignal.setText(images.toString());
fastjson_native_last.setText(json);
}