Android数据解析JSON解析之FastJson解析
程序员文章站
2022-06-17 09:07:07
...
Android数据解析JSON解析之FastJson解析
概述
Fastjson是一个Java语言编写的高性能功能完善的JSON库。它采用一种“假定有序快速匹配”的算法,把JSON Parse的性能提升到极致,是目前Java语言中最快的JSON库.Fastjson接口简单易用,已经被广泛使用在缓存序列化,协议交互,Web输出,Android客户端等多种应用场景。
数据解析操作
1、将json格式的对象转换为Java对象
测试数据
{
"id":2, "name":"大虾",
"price":12.3,
"imagePath":"http://192.168.10.165:8080/L05_Server/images/f1.jpg"
}
代码:
private void jsonToJavaObjectByFastJson() {
// 1 获取或创建json数据
String json = "{\n" +
"\t\"id\":2, \"name\":\"大虾\", \n" +
"\t\"price\":12.3, \n" +
"\t\"imagePath\":\"http://192.168.10.165:8080/L05_Server/images/f1.jpg\"\n" +
"}\n";
// 2 解析JSON数据
ShopInfo shopInfo = JSON.parseObject(json, ShopInfo.class);
// 3 显示数据
tvFastjsonOrignal.setText(json);
tvFastjsonLast.setText(shopInfo.toString());
}
测试结果
08-21 05:50:02.354 4874-4874/com.example.zhangdai.androidjson I/TAG: 详细数据:
id=2
name=大虾
price=12.3
imagePath=http://192.168.10.165:8080/L05_Server/images/f1.jpg
2、将Java对象转换为json对象
测试数据
[
{
"id": 1,
"imagePath": "http://192.168.10.165:8080/f1.jpg",
"name": "大虾1",
"price": 12.3
},
{
"id": 2,
"imagePath": "http://192.168.10.165:8080/f2.jpg",
"name": "大虾2",
"price": 12.5
}
]
代码
private void jsonToJavaListByFastJson() {
String json = "[\n" +
" {\n" +
" \"id\": 1,\n" +
" \"imagePath\": \"http://192.168.10.165:8080/f1.jpg\",\n" +
" \"name\": \"大虾1\",\n" +
" \"price\": 12.3\n" +
" },\n" +
" {\n" +
" \"id\": 2,\n" +
" \"imagePath\": \"http://192.168.10.165:8080/f2.jpg\",\n" +
" \"name\": \"大虾2\",\n" +
" \"price\": 12.5\n" +
" }\n" +
"]";
// 2 解析JSON数据
List<ShopInfo> shops=JSON.parseArray(json,ShopInfo.class);
// 3 显示数据
tvFastjsonOrignal.setText(json);
tvFastjsonLast.setText(shops.toString());
}
测试结果
8-21 05:51:34.314 4874-4874/com.example.zhangdai.androidjson I/TAG: 详细数据:
id=1
name=大虾1
price=12.3
imagePath=http://192.168.10.165:8080/f1.jpg
08-21 05:51:34.314 4874-4874/com.example.zhangdai.androidjson I/TAG: 详细数据:
id=2
name=大虾2
price=12.5
imagePath=http://192.168.10.165:8080/f2.jpg
3、将json格式的数组[]转换为Java对象的List
private void javaToJsonObjectByFastJson() {
// 1 创建一个Java对象
ShopInfo shopInfo = new ShopInfo(1, "鲍鱼", 250.0, "baoyu");
// 2 生成JSON数据
String json=JSON.toJSONString(shopInfo);
// 3 显示数据
tvFastjsonOrignal.setText(shopInfo.toString());
tvFastjsonLast.setText(json);
}
测试结果
4、将Java对象的List转换为json数组[]
private void javaToJsonArrayByFastJson() {
// 1 创建一个Java集合
List<ShopInfo> shops = new ArrayList<>();
ShopInfo baoyu = new ShopInfo(1, "鲍鱼", 250.0, "baoyu");
ShopInfo longxia = new ShopInfo(2, "龙虾", 251.0, "longxia");
shops.add(baoyu);
shops.add(longxia);
// 2 生成JSON数据
String json = JSON.toJSONString(shops);
// 3 显示JSON数据
tvFastjsonOrignal.setText(shops.toString());
tvFastjsonLast.setText(json);
}
测试结果
5、解析复杂数据
测试数据
{
"data": {
"count": 5,
"items": [
{
"id": 45,
"title": "坚果"
},
{
"id": 132,
"title": "炒货"
},
{
"id": 166,
"title": "蜜饯"
},
{
"id": 195,
"title": "果脯"
},
{
"id": 196,
"title": "礼盒"
}
]
},
"rs_code": "1000",
"rs_msg": "success"
}
代码
/*
解析复杂数据
*/
public void fastjsonOfComplex(View view) {
// 1 获取或创建JSON数据
String json = "{\n" +
" \"data\": {\n" +
" \"count\": 5,\n" +
" \"items\": [\n" +
" {\n" +
" \"id\": 45,\n" +
" \"title\": \"坚果\"\n" +
" },\n" +
" {\n" +
" \"id\": 132,\n" +
" \"title\": \"炒货\"\n" +
" },\n" +
" {\n" +
" \"id\": 166,\n" +
" \"title\": \"蜜饯\"\n" +
" },\n" +
" {\n" +
" \"id\": 195,\n" +
" \"title\": \"果脯\"\n" +
" },\n" +
" {\n" +
" \"id\": 196,\n" +
" \"title\": \"礼盒\"\n" +
" }\n" +
" ]\n" +
" },\n" +
" \"rs_code\": \"1000\",\n" +
" \"rs_msg\": \"success\"\n" +
"}";
DataInfo dataInfo = parseObject(json, DataInfo.class);
String mag = dataInfo.getRs_msg();
Log.i("TAG", "mag:" + mag);
String code = dataInfo.getRs_code();
Log.i("TAG", "code:" + code);
int count = dataInfo.getData().getCount();
Log.i("TAG", "count:" + count);
for (int i = 0; i < dataInfo.getData().getItems().size(); i++) {
String title = dataInfo.getData().getItems().get(i).getTitle();
int id = dataInfo.getData().getItems().get(i).getId();
Log.i("TAG", "详细数据:" + "\n"
+ "id=" + id + "\n"
+ "title=" + title + "\n"
);
}
tvFastjsonOrignal.setText(json);
tvFastjsonLast.setText(dataInfo.toString());
}
测试效果
08-21 05:55:53.250 4874-4874/com.example.zhangdai.androidjson I/TAG: mag:success
08-21 05:55:53.250 4874-4874/com.example.zhangdai.androidjson I/TAG: code:1000
08-21 05:55:53.250 4874-4874/com.example.zhangdai.androidjson I/TAG: count:5
08-21 05:55:53.250 4874-4874/com.example.zhangdai.androidjson I/TAG: 详细数据:
id=45
title=坚果
08-21 05:55:53.250 4874-4874/com.example.zhangdai.androidjson I/TAG: 详细数据:
id=132
title=炒货
08-21 05:55:53.250 4874-4874/com.example.zhangdai.androidjson I/TAG: 详细数据:
id=166
title=蜜饯
08-21 05:55:53.250 4874-4874/com.example.zhangdai.androidjson I/TAG: 详细数据:
id=195
title=果脯
08-21 05:55:53.250 4874-4874/com.example.zhangdai.androidjson I/TAG: 详细数据:
id=196
title=礼盒
测试Demo地址
github:https://github.com/TDCQZD/AndroidJson
上一篇: 高性能序列化协议protobuf初探