当前位置: 首页 > 工具软件 > Gson > 使用案例 >

GSON解析

谢俊英
2023-12-01

对于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);


            }
        });

 类似资料: