GSON是Google开发的Java API,用于转换Java对象和Json对象。
Gson提供了两个方法直接用于解析和生成方法,二者都有重载方法:
fromJson():实现反序列化
toJson():实现序列化
测试数据
{
"id":2, "name":"大虾",
"price":12.3,
"imagePath":"http://192.168.10.165:8080/L05_Server/images/f1.jpg"
}
代码:
private void jsonToJavaObjectByGson() {
// 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数据
Gson gson = new Gson();
ShopInfo shopInfo = gson.fromJson(json, ShopInfo.class);
//3、解析每一个数据值
int id = shopInfo.getId();
String name = shopInfo.getName();
double price = shopInfo.getPrice();
String imagePath = shopInfo.getImagePath();
Log.i("TAG", "详细数据:" + "\n"
+ "id=" + id + "\n"
+ "name=" + name + "\n"
+ "price=" + price + "\n"
+ "imagePath=" + imagePath + "\n"
);
// 3 展示数据
tvGsonOrignal.setText(json);
tvGsonLast.setText(shopInfo.toString());
}
打印的详细数据
I/TAG: 详细数据:
id=2
name=大虾
price=12.3 imagePath=http://192.168.10.165:8080/L05_Server/images/f1.jpg
private void javaToJsonObjectByGson() {
// 1 获取或创建Java对象
ShopInfo shopInfo = new ShopInfo(1, "鲍鱼", 250.0, "baoyu");
// 2 生成JSON数据
Gson gson = new Gson();
String json = gson.toJson(shopInfo);
// 3 展示数据
tvGsonOrignal.setText(shopInfo.toString());
tvGsonLast.setText(json);
}
测试效果:
private void javaToJsonArrayByGson() {
// 1 获取或创建Java对象
List 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数据
Gson gson = new Gson();
String json = gson.toJson(shops);
// 3 展示数据
tvGsonOrignal.setText(shops.toString());
tvGsonLast.setText(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 jsonToJavaListByGson() {
// 1 获取或创建JSON数据
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数据
Gson gson = new Gson();
/*
第一种方法
Gson可以直接解析成一个List
*/
List shops = gson.fromJson(json, new TypeToken>() {
}.getType());
for (int i = 0; i < shops.size(); i++) {
String imagePath = shops.get(i).getImagePath();
int id = shops.get(i).getId();
String name = shops.get(i).getName();
double price = shops.get(i).getPrice();
Log.i("TAG", "无数据头的纯数组数据:" + "\n"
+ "id=" + id + "\n"
+ "name=" + name + "\n"
+ "price=" + price + "\n"
+ "imagePath=" + imagePath + "\n"
);
}
/*
第二种方法
*/
//Json的解析类对象
JsonParser jsonParser = new JsonParser();
//将JSON的String 转成一个JsonArray对象
JsonArray jsonArray = jsonParser.parse(json).getAsJsonArray();
ArrayList shopss = new ArrayList<>();
//加强for循环遍历JsonArray
for (JsonElement jsonElement : jsonArray) {
//使用GSON,直接转成Bean对象
ShopInfo shopInfo = gson.fromJson(jsonElement, ShopInfo.class);
//解析每一个数据值
int id = shopInfo.getId();
String name = shopInfo.getName();
double price = shopInfo.getPrice();
String imagePath = shopInfo.getImagePath();
Log.e("TAG", "无数据头的纯数组数据:" + "\n"
+ "id=" + id + "\n"
+ "name=" + name + "\n"
+ "price=" + price + "\n"
+ "imagePath=" + imagePath + "\n"
);
shopss.add(shopInfo);
}
Log.i("TAG", "" + shopss);
// 3 展示数据
tvGsonOrignal.setText(json);
tvGsonLast.setText(shops.toString());
测试效果:
I/TAG: 无数据头的纯数组数据:
id=2
name=大虾2
price=12.5
imagePath=http://192.168.10.165:8080/f2.jpg
E/TAG: 无数据头的纯数组数据:
id=1
name=大虾1
price=12.3
imagePath=http://192.168.10.165:8080/f1.jpg
E/TAG: 无数据头的纯数组数据:
id=2
name=大虾2
price=12.5
imagePath=http://192.168.10.165:8080/f2.jpg
I/TAG: [ShopInfo{id=1, name='大虾1', price=12.3, imagePath='http://192.168.10.165:8080/f1.jpg'}, ShopInfo{id=2, name='大虾2', price=12.5, imagePath='http://192.168.10.165:8080/f2.jpg'}]
源码
/*
有数据头的纯数组数据
*/
public void HeadrList(View v) {
// 1 获取或创建JSON数据
String json = "{\n" +
" \"shop\": [\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" +
" ]\n" +
"}";
// 2 解析JSON数据
Gson gson = new Gson();
//先转JsonObject
JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();
//再转JsonArray 加上数据头
JsonArray jsonArray = jsonObject.getAsJsonArray("shop");
ArrayList shopss = new ArrayList<>();
//循环遍历
for (JsonElement jsonElement : jsonArray) {
ShopInfo shops = gson.fromJson(jsonElement, new TypeToken() {
}.getType());
Log.i("TAG", "" + shops);
int id = shops.getId();
String name = shops.getName();
double price = shops.getPrice();
String imagePath = shops.getImagePath();
Log.i("TAG", "有数据头的纯数组数据:" + "\n"
+ "id=" + id + "\n"
+ "name=" + name + "\n"
+ "price=" + price + "\n"
+ "imagePath=" + imagePath + "\n"
);
shopss.add(shops);
}
Log.i("TAG", "" + shopss);
// 3 展示数据
tvGsonOrignal.setText(json);
tvGsonLast.setText(shopss.toString());
}
测试效果:
I/TAG: 有数据头的纯数组数据:
id=1
name=大虾1
price=12.3
imagePath=http://192.168.10.165:8080/f1.jpg
I/TAG: ShopInfo{id=2, name='大虾2', price=12.5, imagePath='http://192.168.10.165:8080/f2.jpg'}
I/TAG: 有数据头的纯数组数据:
id=2
name=大虾2
price=12.5
imagePath=http://192.168.10.165:8080/f2.jpg
I/TAG: [ShopInfo{id=1, name='大虾1', price=12.3, imagePath='http://192.168.10.165:8080/f1.jpg'}, ShopInfo{id=2, name='大虾2', price=12.5, imagePath='http://192.168.10.165:8080/f2.jpg'}]
测试数据
{
"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 GSONOfComplex(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" +
"}";
// 2 解析JSON数据
Gson gson = new Gson();
DataInfo dataInfo = gson.fromJson(json, DataInfo.class);
String code = dataInfo.getRs_code();
Log.i("TAG", "code:" + code);
String mag = dataInfo.getRs_msg();
Log.i("TAG", "mag:" + mag);
int count = dataInfo.getData().getCount();
Log.i("TAG", "count:" + count);
for (int i = 0; i < dataInfo.getData().getItems().size(); i++) {
int id = dataInfo.getData().getItems().get(i).getId();
String title = dataInfo.getData().getItems().get(i).getTitle();
Log.i("TAG", "详细数据:" + "\n"
+ "id=" + id + "\n"
+ "title=" + title + "\n"
);
}
tvGsonOrignal.setText(json);
tvGsonLast.setText(dataInfo.toString());
}
测试效果:
08-21 05:48:27.404 4874-4874/com.example.zhangdai.androidjson I/TAG: code:1000
08-21 05:48:27.404 4874-4874/com.example.zhangdai.androidjson I/TAG: mag:success
08-21 05:48:27.404 4874-4874/com.example.zhangdai.androidjson I/TAG: count:5
08-21 05:48:27.404 4874-4874/com.example.zhangdai.androidjson I/TAG: 详细数据:
id=45
title=坚果
08-21 05:48:27.404 4874-4874/com.example.zhangdai.androidjson I/TAG: 详细数据:
id=132
title=炒货
08-21 05:48:27.404 4874-4874/com.example.zhangdai.androidjson I/TAG: 详细数据:
id=166
title=蜜饯
08-21 05:48:27.404 4874-4874/com.example.zhangdai.androidjson I/TAG: 详细数据:
id=195
title=果脯
08-21 05:48:27.404 4874-4874/com.example.zhangdai.androidjson I/TAG: 详细数据:
id=196
title=礼盒
测试数据
{
"code": 0,
"list": {
"0": {
"aid": "6008965",
"author": "哔哩哔哩番剧",
"coins": 170,
"copyright": "Copy",
"create": "2016-08-25 21:34"
},
"1": {
"aid": "6008938",
"author": "哔哩哔哩番剧",
"coins": 404,
"copyright": "Copy",
"create": "2016-08-25 21:33"
}
}
}
源码:
/*
解析特殊数据
*/
public void GSONOfSpecial(View view) {
// 1 获取或创建JSON数据
String json = "{\n" +
" \"code\": 0,\n" +
" \"list\": {\n" +
" \"0\": {\n" +
" \"aid\": \"6008965\",\n" +
" \"author\": \"哔哩哔哩番剧\",\n" +
" \"coins\": 170,\n" +
" \"copyright\": \"Copy\",\n" +
" \"create\": \"2016-08-25 21:34\"\n" +
" },\n" +
" \"1\": {\n" +
" \"aid\": \"6008938\",\n" +
" \"author\": \"哔哩哔哩番剧\",\n" +
" \"coins\": 404,\n" +
" \"copyright\": \"Copy\",\n" +
" \"create\": \"2016-08-25 21:33\"\n" +
" }\n" +
" }\n" +
"}";
// 2 解析JSON数据
Gson gson = new Gson();
FilmData filmInfo = gson.fromJson(json, FilmData.class);
int code = filmInfo.getCode();
String create1 = filmInfo.getList().get_$0().getCreate();
String copyright1 = filmInfo.getList().get_$0().getCopyright();
int coins1= filmInfo.getList().get_$0().getCoins();
String author1 =filmInfo.getList().get_$0().getAuthor();
String aid1 = filmInfo.getList().get_$0().getAid();
Log.i("TAG", "0Bean:" + "\n"
+ "aid=" + aid1 + "\n"
+ "author=" + author1 + "\n"
+ "coins=" + coins1 + "\n"
+ "copyright=" + copyright1 + "\n"
+ "create=" + create1 + "\n"
);
String create2 = filmInfo.getList().get_$1().getCreate();
String copyright2 = filmInfo.getList().get_$1().getCopyright();
int coins2= filmInfo.getList().get_$1().getCoins();
String author2 =filmInfo.getList().get_$1().getAuthor();
String aid2 = filmInfo.getList().get_$1().getAid();
Log.i("TAG", "1Bean:" + "\n"
+ "aid=" + aid2 + "\n"
+ "author=" + author2 + "\n"
+ "coins=" + coins2 + "\n"
+ "copyright=" + copyright2 + "\n"
+ "create=" + create2 + "\n"
);
tvGsonOrignal.setText(json);
tvGsonLast.setText(filmInfo.toString());
}
测试效果:
08-21 05:49:00.817 4874-4874/com.example.zhangdai.androidjson I/TAG: 0Bean:
aid=6008965
author=哔哩哔哩番剧
coins=170
copyright=Copy
create=2016-08-25 21:34
08-21 05:49:00.817 4874-4874/com.example.zhangdai.androidjson I/TAG: 1Bean:
aid=6008938
author=哔哩哔哩番剧
coins=404
copyright=Copy
create=2016-08-25 21:33
GSON解析Json数据比较常用,使用GsonFormat插件还可以省去写数据bean类的麻烦。