GSON解析

对于GSON来说,比较容易解析简单数据,但复杂数据就比较难了
将json转换为对象

 final String jsons="  {\"temp\": \"20℃/30℃\",\"weather\": \"晴转多云\",\"name\": \"上海\",\"wind\": \"1级\"}";
        gson1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Gson gson=new Gson();
                com.example.demo.jsons jsons1 = gson.fromJson(jsons, com.example.demo.jsons.class);
                zq.setText(jsons);
                 zh.setText(jsons1.toString());
            }
        });
public class jsons {
    private String temp;
    private String weather;


    @Override
    public String toString() {
        return "jsons{" +
                "temp='" + temp + '\'' +
                ", weather='" + weather + '\'' +
                '}';
    }

    public jsons(String temp, String weather) {
        this.temp = temp;
        this.weather = weather;
    }

    public String getTemp() {
        return temp;
    }

    public void setTemp(String temp) {
        this.temp = temp;
    }

    public String getWeather() {
        return weather;
    }

    public void setWeather(String weather) {
        this.weather = weather;
    }
}

将数组转换为对象


        final String gson23="[\n" +
                "    {\n" +
                "        \"id\": 1,\n" +
                "        \"image\": \"http://123\",\n" +
                "        \"name\": \"大虾1\",\n" +
                "        \"price\": 12.3\n" +
                "    },\n" +
                "    {\n" +
                "        \"id\": 2,\n" +
                "        \"image\": \"htpp://123.jpg\",\n" +
                "        \"name\": \"大虾2\",\n" +
                "        \"price\": 12\n" +
                "    }\n" +
                "]";
 gson2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Gson gsonss2=new Gson();
                Type list=new TypeToken<List<jsonsz>>(){}.getType();
                List<jsonsz> o = gsonss2.fromJson(gson23,list);

                zq.setText(gson23);
                zh.setText(o.toString());
                //使用Gson进行解析
            }
        });

public class jsonsz {


    /**
     * id : 1
     * image : http://123
     * name : 大虾1
     * price : 12.3
     */

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

    public int getId() {
        return id;
    }

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

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    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;
    }

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

将对象转换为json

public class jsons {
    private String temp;
    private String weather;


    @Override
    public String toString() {
        return "jsons{" +
                "temp='" + temp + '\'' +
                ", weather='" + weather + '\'' +
                '}';
    }

    public jsons(String temp, String weather) {
        this.temp = temp;
        this.weather = weather;
    }

    public String getTemp() {
        return temp;
    }

    public void setTemp(String temp) {
        this.temp = temp;
    }

    public String getWeather() {
        return weather;
    }

    public void setWeather(String weather) {
        this.weather = weather;
    }
}

 gson4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jsons jsons1=new jsons("鲤鱼","123");
                Gson gson=new Gson();
                String json=gson.toJson(jsons1);
                zh.setText(json);
            }
        });

将对象转换为数组

package com.example.demo;

public class jsonsz {


    /**
     * id : 1
     * image : http://123
     * name : 大虾1
     * price : 12.3
     */

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

    public int getId() {
        return id;
    }

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

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    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;
    }

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

 gson5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jsons jsons1=new jsons("鲤鱼","123");
                jsons  jsons2=new jsons("aaa","ccc");
                List<jsons> list=new ArrayList<>();
                list.add(jsons1);
                list.add(jsons2);
                Gson gson=new Gson();
                String json=gson.toJson(list);
                zh.setText(json);


            }
        });

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值