谷歌Gson入门篇

来源:互联网 发布:淘宝店铺店标在线制作 编辑:程序博客网 时间:2024/04/28 18:04

依赖谷歌最新版本的类库:

compile 'com.google.code.gson:gson:2.8.0'

1、使用Gson将对象转换成字符串前,先创建几个对象

(1)

public class People {    private String name;    private String sex;    private String height;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getSex() {        return sex;    }    public void setSex(String sex) {        this.sex = sex;    }    public String getHeight() {        return height;    }    public void setHeight(String height) {        this.height = height;    }}
(2)

public class Animal {    private String eyes;    private String feet;    public String getEyes() {        return eyes;    }    public void setEyes(String eyes) {        this.eyes = eyes;    }    public String getFeet() {        return feet;    }    public void setFeet(String feet) {        this.feet = feet;    }}
(3)

public class Peoples {    private List<People> peopleList;    public List<People> getPeopleList() {        return peopleList;    }    public void setPeopleList(List<People> peopleList) {        this.peopleList = peopleList;    }}

(4)

public class World {    private People people;    private Animal animal;    public People getPeople() {        return people;    }    public void setPeople(People people) {        this.people = people;    }    public Animal getAnimal() {        return animal;    }    public void setAnimal(Animal animal) {        this.animal = animal;    }}
2、一般我们会将信息封装成一个对象,然后再发送给服务器

(1)只发送一个对象

People people1 = new People();people1.setName("people1");people1.setSex("man1");people1.setHeight("171cm");Gson gson1 = new Gson();String str1 = gson1.toJson(people1);Log.d("execute", str1);
D/execute: {"height":"171cm","name":"people1","sex":"man1"}

(2)同一对象的集合

People people2 = new People();people2.setName("people2");people2.setSex("female1");people2.setHeight("122cm");List<People> peopleList = new ArrayList<>();peopleList.add(people1);peopleList.add(people2);Gson gson2 = new Gson();String str2 = gson2.toJson(peopleList);Log.d("execute", str2);
 D/execute: [{"height":"171cm","name":"people1","sex":"man1"},{"height":"122cm","name":"people2","sex":"female1"}]

(3)不同对象

Gson gson3 = new Gson();People people3 = new People();people3.setName("java");people3.setSex("man");people3.setHeight("10mm");Animal animal3 = new Animal();animal3.setEyes("double");animal3.setFeet("three");World world1 = new World();world1.setAnimal(animal3);World world2 = new World();world2.setPeople(people3);world2.setAnimal(animal3);List<World> worldList = new ArrayList<>();worldList.add(world1);worldList.add(world2);String str3 = gson3.toJson(worldList);Log.d("execute", str3);
[{"animals":{"eyes":"double","feet":"three"}},{"animals":{"eyes":"double","feet":"three"},"people":{"height":"10mm","name":"java","sex":"man"}}]

3、因为接收到服务器发送过来的对象是字符串,因此我们可以通过fromJson转换成对象

(1)对于2、(1)转换的字符串看成服务器发送过来的,那么

Gson gson4 = new Gson();People people4 = gson4.fromJson(str1, People.class);Log.d("execute", people4.getName()+"="+people4.getSex()+"="+people4.getHeight());
people1=man1=171cm

(2)接收同一对象的集合

a、以前这样用可以,不知道现在错在哪,改天改下

Gson gson5 = new Gson();List<People> peopleList2 = new ArrayList<>();Peoples peoples = gson5.fromJson(str2, Peoples.class);peopleList2 = peoples.getPeopleList();for (int i = 0; i< peopleList2.size(); i++) {    Log.d("execute",peopleList2.get(i).getName()+"=="+ peopleList2.get(i).getSex()+"="+ peopleList2.get(i).getHeight());}

D/execute: people1==man1==171cm

D/execute: people2==female1==122cm

b、目前正确方法

Gson gson5 = new Gson();// Type type = new TypeToken<List<Peoples>>(){}.getType();People[] peoples = gson5.fromJson(str2, People[].class);for (int i=0; i<peoples.length; i++) {    Log.d("execute", peoples[i].getName()+"=="+peoples[i].getSex()+"=="+peoples[i].getHeight());}

D/execute: people1==man1==171cm

D/execute: people2==female1==122cm

c、对与上面b的方法是数组有限,你我们希望能用集合来发送接收对象,下面是我另外测试写的,跟上面没差,只是改了构造方法

个人还是习惯这种写法

People people1 = new People("Jack", 22, "male");People people2 = new People("Macle", 44, "female");Peoples peoples = new Peoples();List<People> peopleList = new ArrayList<>();peopleList.add(people1);peopleList.add(people2);peoples.setPeopleList(peopleList);Gson gson = new Gson();String str = gson.toJson(peoples);Peoples peoples1 = gson.fromJson(str, Peoples.class);for (int i=0; i<peoples1.getPeopleList().size(); i++) {    Log.d("execute", peoples1.getPeopleList().get(i).getName()+"=="+peoples1.getPeopleList().get(i).getSex()+"=="+peoples1.getPeopleList().get(i).getAge());}

(3)那么一个对象里又有其他对象,这样嵌套的就把3、(2)理解就会掌握了。实在不会就多动手呗。

其实学会上面的简单知识点,就可以接收跟发送数据了。

那下篇会进一步学习Gson的其他牛逼的功能。

0 0
原创粉丝点击