android Gson 使用的四种方式

相对于较为传统的Json解析来说,google共享的开源Gson在解析速度和所使用的内存在有着明显的优势,虽然说阿里巴巴也提供了fastgson包,但是它跟Gson的处理速度大同小异,只是底层实现的原理不同,在这里就来学习一下怎么使用google的开源包Gson(在学习之间你应该下载好google gson包)

Gson的解析非常简单,但是它的解析规则是必须有一个bean文件,这个bean文件的内容跟JSon数据类型是一一对应的

解析的三个步骤:
1.获取数据或者创建数据
2.解析数据
3.显示到控件上去

一、将json格式的字符串{}转换为java对象

这里写图片描述

 //将json对象转换为java对象
    private void jsonToJavaObjectByGson() {
        //获取或者创建json数据
        String json = "{\n" +
                "    \"id\":2,\n" +
                "    \"name\":\"猴哥\",\n" +
                "    \"price\":12.3,\n" +
                "     \"imgPath\":\"http://img00.hc360.com/cloth/201206/201206191116426249.jpg\"\n" +
                "\n" +
                "}";
        //解析数据
        Gson gson = new Gson();
        shopInfo shopInfo = gson.fromJson(json, shopInfo.class);

        //展示数据
        mtest01.setText("原始数据:" + json);
        mtest02.setText("解析后数据:" + shopInfo.toString());
    }

shopInfo .java

package com.example.gsons.baed;

/**
 * Created by Myzg2 on 2017/7/30.
 */

public class shopInfo {

    /**
     * id : 2
     * imgPath : http://img00.hc360.com/cloth/201206/201206191116426249.jpg
     * name : 猴哥
     * price : 12.3
     */

    private int id;
    private String imgPath;
    private String name;
    private double price;

    public shopInfo(int id, String imgPath, String name, double price) {
        this.id = id;
        this.imgPath = imgPath;
        this.name = name;
        this.price = price;
    }

    @Override
    public String toString() {
        return "shopInfo{" +
                "id=" + id +
                ", imgPath='" + imgPath + '\'' +
                ", name='" + name + '\'' +
                ", price=" + price +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getImgPath() {
        return imgPath;
    }

    public void setImgPath(String imgPath) {
        this.imgPath = imgPath;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

}

二、将json数组转换为java对象的集合

这里写图片描述


    //将json数组转换为java对象的集合
    private void jsonToJavaarByGson() {
        //获取数据或者创建数据
        String json = "[\n" +
                "    {\n" +
                "        \"id\": 1,\n" +
                "        \"imgPath\": \"http://img00.hc360.com/cloth/201206/201206191116426249.jpg\",\n" +
                "        \"name\": \"猴哥\",\n" +
                "        \"price\": 12.3\n" +
                "    },\n" +
                "    {\n" +
                "        \"id\": 2,\n" +
                "        \"imgPath\": \"http://img00.hc360.com/cloth/201206/201206191116426249.jpg\",\n" +
                "        \"name\": \"八戒\",\n" +
                "        \"price\": 63.2\n" +
                "    }\n" +
                "]";
        Gson gson = new Gson();
        List<shopInfo> o = gson.fromJson(json, new TypeToken<List<shopInfo>>() {
        }.getType());
        //展示数据
        mtest01.setText("原始数据:" + json);
        mtest02.setText("解析后数据:" + o.toString());

    }

三、将java对象转化为Json对象

这里写图片描述


    //将java对象转化为Json对象
    private void jsonjava() {
        //获取或创建java对象
        shopInfo shopinfo = new shopInfo(1, "xiguo", "西瓜", 23.2);

        //生成json数据
        Gson g = new Gson();
        String s = g.toJson(shopinfo);
        //展示数据
        mtest01.setText("原始数据:" + shopinfo);
        mtest02.setText("解析后数据:" + s);

    }

四、将java集合转化为Json数据

这里写图片描述

  //将java集合转化为Json数据

    private void jsonListjava() {
        //创建java对象
        List<shopInfo> list = new ArrayList<>();
        shopInfo shopInfo = new shopInfo(1, "xiguo", "西瓜", 23.2);
        shopInfo shopInfo1 = new shopInfo(1, "pingguo", "苹果", 36.2);
        list.add(shopInfo);
        list.add(shopInfo1);
        //生成json数据
        Gson gson=new Gson();
        String s = gson.toJson(list);
        //展示数据
        mtest01.setText("原始数据:" + list.toString());
        mtest02.setText("解析后数据:" + s);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值