<pre class="java" name="code">package com.world.hello.gsonexample;
import java.util.ArrayList;
import java.util.HashMap;
/**
* 小孩实体类
* Created by chengguo on 2016/3/21.
*/
public class Child {
private int id;
private String name;
private String sex;
private int age;
//儿童玩具
private ArrayList<String> toys;
//儿童玩具的map
private HashMap<String, String> toysMap = new HashMap<String, String>();
//小孩的书籍
private ArrayList<Book> books = new ArrayList<Book>();
public ArrayList<Book> getBooks() {
return books;
}
public void setBooks(ArrayList<Book> books) {
this.books = books;
}
public HashMap<String, String> getToysMap() {
return toysMap;
}
public void setToysMap(HashMap<String, String> toysMap) {
this.toysMap = toysMap;
}
public ArrayList<String> getToys() {
return toys;
}
public void setToys(ArrayList<String> toys) {
this.toys = toys;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package com.world.hello.gsonexample;
/**
* 书实体类
* Created by chengguo on 2016/3/21.
*/
public class Book {
private String name;
private String price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
package com.world.hello.gsonexample;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Gson gson = new Gson();
Child child = new Child();
child.setId(1);
child.setAge(10);
child.setName("小孩A");
child.setSex("男");
ArrayList<String> toys = new ArrayList<String>();
toys.add("小车");
toys.add("皮卡丘");
toys.add("奥特曼");
toys.add("火影忍者");
child.setToys(toys);
HashMap<String, String> toysMap = new HashMap<String, String>();
toysMap.put("1", "小车2");
toysMap.put("2", "皮卡丘2");
toysMap.put("3", "奥特曼2");
toysMap.put("4", "火影忍者2");
child.setToysMap(toysMap);
ArrayList<Book> books = new ArrayList<Book>();
for (int i = 0; i < 3; i++) {
Book book = new Book();
book.setName("格林童话" + i);
book.setPrice("价格:" + i + "$");
books.add(book);
}
child.setBooks(books);
Log.i("child", gson.toJson(child));
}
}
{
"age": 10,
"books": [
{
"name": "格林童话0",
"price": "价格:0$"
},
{
"name": "格林童话1",
"price": "价格:1$"
},
{
"name": "格林童话2",
"price": "价格:2$"
}
],
"id": 1,
"name": "小孩A",
"sex": "男",
"toys": [
"小车",
"皮卡丘",
"奥特曼",
"火影忍者"
],
"toysMap": {
"1": "小车2",
"2": "皮卡丘2",
"3": "奥特曼2",
"4": "火影忍者2"
}
}
package com.world.hello.gsonexample;
import com.google.gson.annotations.SerializedName;
import java.util.List;
/**
* Created by chengguo on 2016/3/21.
*/
public class Child2 {
/**
* age : 10
* books : [{"name":"格林童话0","price":"价格:0$"},{"name":"格林童话1","price":"价格:1$"},{"name":"格林童话2","price":"价格:2$"}]
* id : 1
* name : 小孩A
* sex : 男
* toys : ["小车","皮卡丘","奥特曼","火影忍者"]
* toysMap : {"1":"小车2","2":"皮卡丘2","3":"奥特曼2","4":"火影忍者2"}
*/
@SerializedName("age")
private int age2;
private int id;
private String name;
private String sex;
/**
* 1 : 小车2
* 2 : 皮卡丘2
* 3 : 奥特曼2
* 4 : 火影忍者2
*/
private ToysMapBean toysMap;
/**
* name : 格林童话0
* price : 价格:0$
*/
private List<BooksBean> books;
private List<String> toys;
public int getAge2() {
return age2;
}
public void setAge2(int age2) {
this.age2 = age2;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 ToysMapBean getToysMap() {
return toysMap;
}
public void setToysMap(ToysMapBean toysMap) {
this.toysMap = toysMap;
}
public List<BooksBean> getBooks() {
return books;
}
public void setBooks(List<BooksBean> books) {
this.books = books;
}
public List<String> getToys() {
return toys;
}
public void setToys(List<String> toys) {
this.toys = toys;
}
public static class ToysMapBean {
@SerializedName("1")
private String value1;
@SerializedName("2")
private String value2;
@SerializedName("3")
private String value3;
@SerializedName("4")
private String value4;
public String getValue1() {
return value1;
}
public void setValue1(String value1) {
this.value1 = value1;
}
public String getValue2() {
return value2;
}
public void setValue2(String value2) {
this.value2 = value2;
}
public String getValue3() {
return value3;
}
public void setValue3(String value3) {
this.value3 = value3;
}
public String getValue4() {
return value4;
}
public void setValue4(String value4) {
this.value4 = value4;
}
}
public static class BooksBean {
private String name;
private String price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
}
//网络访问成功返回数据的方法
public void onResponse(String response) {
//成功获取网络json数据
Child2 child2 = gson.fromJson(response, Child2.class);}
只需要一句话就能得到Child2对象,这样就免去了少则几十行,多则成百上千行的json解析代码。上面是获取到一个对象数据,接下来获取list数据。
List<Child2> child2List = gson.fromJson(response, new TypeToken<List<Child2>>(){}.getType());
可以看到上面的代码使用了TypeToken,它是gson提供的数据类型转换器,也是只需要一句代码,就能将json数据转化为list 数据,可以支持各种数据集合类型转换。
注意:当属性值为int型,它在json中的数据为 空 时,gson解析默认为 0;
当属性值为String型,它在json中的数据为 空 时 ,gson解析默认为 字符串“null”;
Gson的基本使用就是这么多了。希望对你的学习有所帮助。
原文:http://blog.csdn.net/oqihaogongyuan/article/details/50944755