json(Javascript Object Notation)是目前比较常用的android端和服务器端的轻量级的数据交换格式,典型的如下格式:
persons: [{"address":"广州","name":"xiaoluo","id":1},{"address":"上海","name":"android","id":2}]
persons是一个json对象,最外层是[],这表示是一个数组或者list,{}表示一个对象,说明整体上persons是一个对象的数组即list<person>
{}里面是对象的各种属性,格式就是键值对,值如果是String类型的话要加上“”。
把自己的数据比如object,list或者map等转化成json对象的称之为json数据的序列化,反之把json对象转换为我们自己的数据格式的过程称之为json数据的反序列化。
本文主要讲如何利用google的Gson库来进行序列化和反序列化,主要参考的博文如下,用的也是该博文里面的例子,该篇博文讲的比较详细:
http://www.cnblogs.com/xiaoluo501395377/p/3446605.html
在android stdio里面建立好工程后,首先建立一个javabean即Person类:
public class Person
{
private int id;
private String name;
private String address;
public Person()
{
}
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 getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
public Person(int id, String name, String address)
{
super();
this.id = id;
this.name = name;
this.address = address;
}
@Override
public String toString()
{
return "Person [id=" + id + ", name=" + name + ", address=" + address
+ "]";
}
}
然后建立一个JsonService类
public class JsonService
{
public Person getPerson()
{
Person person = new Person(1, "xiaoluo", "广州");
return person;
}
public List<Person> getPersons()
{
List<Person> persons = new ArrayList<Person>();
Person person = new Person(1, "xiaoluo", "广州");
Person person2 = new Person(2, "android", "上海");
persons.add(person);
persons.add(person2);
return persons;
}
public List<String> getString()
{
List<String> list = new ArrayList<String>();
list.add("广州");
list.add("上海");
list.add("北京");
return list;
}
public List<Map<String, String>> getMapList()
{
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
Map<String, String> map1 = new HashMap<String, String>();
map1.put("id", "001");
map1.put("name", "xiaoluo");
map1.put("age", "20");
Map<String, String> map2 = new HashMap<String, String>();
map2.put("id", "002");
map2.put("name", "android");
map2.put("age", "33");
list.add(map1);
list.add(map2);
return list;
}
}
然后建立一个JsonTestActivity的Activity
public class JsonTestActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_json_test);
Gson gson = new Gson();
JsonService jsonService = new JsonService();
Person person = jsonService.getPerson();
System.out.println("person: " + gson.toJson(person));
// 对于Object类型,使用 fromJson(String, Class)方法来将Json对象转换成Java对象
Person person2 = gson.fromJson(gson.toJson(person), Person.class);
System.out.println(person2);
System.out.println("------------------------------------------------");
List<Person> persons = jsonService.getPersons();
System.out.println("persons: " + gson.toJson(persons));
/*
* 对于泛型对象,使用fromJson(String, Type)方法来将Json对象转换成对应的泛型对象
* new TypeToken<>(){}.getType()方法
*/
List<Person> persons2 = gson.fromJson(gson.toJson(persons), new TypeToken<List<Person>>(){}.getType());
System.out.println(persons2);
System.out.println("------------------------------------------------");
List<String> list = jsonService.getString();
System.out.println("String---->" + gson.toJson(list));
List<String> list2 = gson.fromJson(gson.toJson(list), new TypeToken<List<String>>(){}.getType());
System.out.println("list2---->" + list2);
System.out.println("------------------------------------------------");
List<Map<String, String>> listMap = jsonService.getMapList();
System.out.println("Map---->" + gson.toJson(listMap));
List<Map<String, String>> listMap2 = gson.fromJson(gson.toJson(listMap), new TypeToken<List<Map<String, String>>>(){}.getType());
System.out.println("listMap2---->" + listMap2);
System.out.println("------------------------------------------------");
}
}
这个时候要导入Google的Gson包,在gradle里面加入compile 'com.google.code.gson:gson:2.3.1'即可导入Gson包,或者去https://github.com/google/gson下载压缩包,加到lib里面,然后add as library即可。
最后运行程序,在DDMS里面日志输出可以看到测试结果,截图如下: