首先说一下GsonFormat的用法:
1.点击AS里的设置
2.安装完成之后重启Studio,新建号解析类后,按Alt键+S键,
3.把Json数据粘贴到编辑框里,点击OK即可
4.导入Gson
把数据放到Bean中
这里告诉大家个最简单的方法,这也是我从万千文章当中找出来的,但是我已经找不到他的地址了
直接贴代码(工具类)
public abstract class Json {
private static Json json;
Json() {
}
public static Json get() {
if (json == null) {
json = new GsonImpl();
}
return json;
}
public abstract String toJson(Object src);
public abstract <T> T toObject(String json, Class<T> claxx);
public abstract <T> T toObject(byte[] bytes, Class<T> claxx);
public abstract <T> List<T> toList(String json, Class<T> claxx);
}
public class GsonImpl extends Json {
private Gson gson = new Gson();
@Override
public String toJson(Object src) {
return gson.toJson(src);
}
@Override
public <T> T toObject(String json, Class<T> claxx) {
return gson.fromJson(json, claxx);
}
@Override
public <T> T toObject(byte[] bytes, Class<T> claxx) {
return gson.fromJson(new String(bytes), claxx);
}
@Override
public <T> List<T> toList(String json, Class<T> claxx) {
Type type = new TypeToken<ArrayList<T>>() {}.getType();
List<T> list = gson.fromJson(json, type);
return list;
}
使用方式
TestBean bean = GsonImpl.get().toObject(json, TestBean.class);
测试数据:
测试用例
最后给大家些测试用例
测试1: 简单模型测试
{
"name": "王五",
"gender": "man",
"age": 15,
"height": "140cm"
}
测试2: 多种模型测试
{
"name": "王五",
"gender": "man",
"age": 15,
"height": "140cm",
"addr": {
"province": "fujian",
"city": "quanzhou",
"code": "300000"
},
"hobby": [
{
"name": "billiards",
"code": "1"
},
{
"name": "computerGame",
"code": "2"
}
]
}
测试3: 测试List泛型
{ "list": [1,2,3,4] }
测试4: 测试多重嵌套
{
"kind": "youtube#searchListResponse",
"etag": "\"k1sYjErg4tK7WaQQxvJkW5fVrfg/usRLzp_l5ss-L8Yb9ScY-VDAEOo\"",
"nextPageToken": "CAIQAA",
"pageInfo": {
"totalResults": 1000000,
"resultsPerPage": 2
},
"items": [
{
"kind": "youtube#searchResult",
"etag": "\"k1sYjErg4tK7WaQQxvJkW5fVrfg/D0nlqgSgFFEQnTduJtwcQZ3Hc78\"",
"id": {
"kind": "youtube#video",
"videoId": "5ixFWqZGZr8"
},
"snippet": {
"publishedAt": "2014-10-16T19:43:41.000Z",
"channelId": "UC9-3c4LzdzT_HvW3Xuti9wg",
"title": "Q&A WITH HARRY | PRANK CALLS!",
"description": "Can we smash 4000 likes for this Q&A!? My Flatmate Harold - http://www.youtube.com/W2S Buy Cheap and Instant Coins here: http://goo.gl/IZyNg2 Use code ...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/5ixFWqZGZr8/default.jpg"
},
"medium": {
"url": "https://i.ytimg.com/vi/5ixFWqZGZr8/mqdefault.jpg"
},
"high": {
"url": "https://i.ytimg.com/vi/5ixFWqZGZr8/hqdefault.jpg"
}
},
"channelTitle": "Calfreezy",
"liveBroadcastContent": "none"
}
},
{
"kind": "youtube#searchResult",
"etag": "\"k1sYjErg4tK7WaQQxvJkW5fVrfg/R5nmI-OBXmHyiXsGtMC3J6IUlBs\"",
"id": {
"kind": "youtube#video",
"videoId": "8KgzATFR0As"
},
"snippet": {
"publishedAt": "2014-09-03T12:49:15.000Z",
"channelId": "UC8_yRRDIGFaJEC7NOc1qTMw",
"title": "Harry at 30 Documentary",
"description": "In celebration of Prince Harry's 30th birthday, ITV have released a new documentary celebrating the past three decades of his life. Featuring archive footage...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/8KgzATFR0As/default.jpg"
},
"medium": {
"url": "https://i.ytimg.com/vi/8KgzATFR0As/mqdefault.jpg"
},
"high": {
"url": "https://i.ytimg.com/vi/8KgzATFR0As/hqdefault.jpg"
}
},
"channelTitle": "",
"liveBroadcastContent": "none"
}
}
]
}
测试 5
{
"_r":0,
"err":"None",
"photo":"http://xxxx.com/photo.jpg"
}
测试 6
{
"mesg": "获取商品列表(某类型)",
"data": {
"list": [
{
"y_price": "4",
"selled": "100",
"score": "5",
"package": "200g",
"isSpecial": "0",
"n_price": "4",
"nation": "中国",
"version": "1",
"productId": "1",
"content": "180g",
"imgUrl": "/SKU_PIC/jiee/KSFTM1.jpg",
"productDesc": "方便面,好吃不贵",
"bestBeforeDate": "24个月",
"isDiscount": "0",
"moreInfoUrl": "/SKU_PIC/jiee/KSFTM1.jpg",
"isPromotion": "0",
"productName": "大食桶红烧牛肉面-1",
"productionPlace": "0"
},
{
"y_price": "4",
"selled": "0",
"score": "5",
"package": "200g",
"isSpecial": "0",
"n_price": "4",
"nation": "中国",
"version": "1",
"productId": "9",
"content": "180g",
"imgUrl": "/SKU_PIC/jiee/KSFTM1.jpg",
"productDesc": "方便面,好吃不贵",
"bestBeforeDate": "24个月",
"isDiscount": "0",
"moreInfoUrl": "/SKU_PIC/jiee/KSFTM1.jpg",
"isPromotion": "0",
"productName": "大食桶红烧牛肉面-2",
"productionPlace": "0"
},
{
"y_price": "4",
"selled": "0",
"score": "5",
"package": "200g",
"isSpecial": "0",
"n_price": "4",
"nation": "中国",
"version": "1",
"productId": "17",
"content": "180g",
"imgUrl": "/SKU_PIC/jiee/KSFTM1.jpg",
"productDesc": "方便面,好吃不贵",
"bestBeforeDate": "24个月",
"isDiscount": "0",
"moreInfoUrl": "/SKU_PIC/jiee/KSFTM1.jpg",
"isPromotion": "0",
"productName": "大食桶红烧牛肉面-3",
"productionPlace": "0"
},
{
"y_price": "4",
"selled": "0",
"score": "5",
"package": "200g",
"isSpecial": "0",
"n_price": "4",
"nation": "中国",
"version": "1",
"productId": "25",
"content": "180g",
"imgUrl": "/SKU_PIC/jiee/KSFTM1.jpg",
"productDesc": "方便面,好吃不贵",
"bestBeforeDate": "24个月",
"isDiscount": "0",
"moreInfoUrl": "/SKU_PIC/jiee/KSFTM1.jpg",
"isPromotion": "0",
"productName": "大食桶红烧牛肉面-4",
"productionPlace": "0"
},
{
"y_price": "4",
"selled": "0",
"score": "5",
"package": "200g",
"isSpecial": "0",
"n_price": "4",
"nation": "中国",
"version": "1",
"productId": "25",
"content": "180g",
"imgUrl": "/SKU_PIC/jiee/KSFTM1.jpg",
"productDesc": "方便面,好吃不贵",
"bestBeforeDate": "24个月",
"isDiscount": "0",
"moreInfoUrl": "/SKU_PIC/jiee/KSFTM1.jpg",
"isPromotion": "0",
"productName": "大食桶红烧牛肉面-5",
"productionPlace": "0"
},
{
"y_price": "4",
"selled": "0",
"score": "5",
"package": "200g",
"isSpecial": "0",
"n_price": "4",
"nation": "中国",
"version": "1",
"productId": "41",
"content": "180g",
"imgUrl": "/SKU_PIC/jiee/KSFTM1.jpg",
"productDesc": "方便面,好吃不贵",
"bestBeforeDate": "24个月",
"isDiscount": "0",
"moreInfoUrl": "/SKU_PIC/jiee/KSFTM1.jpg",
"isPromotion": "0",
"productName": "大食桶红烧牛肉面-6",
"productionPlace": "0"
}
]
},
"isSuccess": 0
}