Android笔记:Gsonformat解析Json数据

首先说一下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
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值