在android开发中经常需要操作json数据,利用谷歌提供的gson开源工具,可以很方便的解析和生成json数据
1.添加gson-1.6.jar到工程中。
2.添加接下来解析以及用来生成json数据对应的实体类,实体类在这里就不一一贴出来了,最后会给出源码下载的连接的。
源码下载:http://download.csdn.net/detail/mockingbirds/8147077
将student对象转换成json字符串
Student student = new Student(2,"zhangsan","1232","222222",22);
String studentGson = new Gson().toJson(student);
System.out.println(studentGson);
System.out.println("----------------------------------------------");
String json = "{\"id\":2,\"name\":\"zhangsan\",\"pass\":\"1232\",\"phone\":\"222222\",\"age\":22}";
Student student2 = new Gson().fromJson(json, Student.class);
System.out.println(student2.toString());
String json2 = "[{\"name\":\"haha\",\"age\":30},{\"secondname\":\"test\",\"age\":33}]";
JsonReader reader = new JsonReader(new StringReader(json2));
reader.beginArray();
while (reader.hasNext()) {
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
String value = reader.nextString();
System.out.println("name:="+name+"---"+"value="+value);
}
reader.endObject();
}
reader.endArray();
String json3 = "{\"name\":\"fine\",\"age\":55,\"male\":true,\"address\":{\"street\":\"fourth\",\"city\":\"huizhou\",\"country\":\"china\"}}";
Person person = new Gson().fromJson(json3,Person.class);
System.out.println(person);
Listlist = new ArrayList();
Person p1 = new Person("张三",55,false,new Address("once","惠州","广东"));
Person p2 = new Person("wang三",55,false,new Address("fotr","噶ung州","广速度"));
Person p3 = new Person("张里",51,false,new Address("sedf","额州","梵蒂冈东"));
Person p4 = new Person("马甲的",44,false,new Address("asdfr","请州","广东"));
list.add(p1);
list.add(p2);
list.add(p3);
list.add(p4);
Type typeList = new TypeToken>() {}.getType();
Gson gson = new Gson(); Gson gson = new Gson();
// String jsonStr = gson.toJson(list, typeList);
// System.out.println(jsonStr);
/*** 采用gson库解析json数组* 将json数组解析成list* 一个json数组里边包含了(一个对象嵌套另一个对象)的解析*/
String s = "[{\"name\":\"张里\",\"age\":51,\"male\":false,\"address\":{\"street\":\"sedf\",\"city\":\"额州\",\"country\":\"梵蒂冈东\"}},{\"name\":\"马甲的\",\"age\":44,\"male\":false,\"address\":{\"street\":\"asdfr\",\"city\":\"请州\",\"country\":\"广东\"}}]";
Type typeList2 = new TypeToken>() {}.getType();
Gson gson2 = new Gson();
Listlist2 = gson2.fromJson(s,typeList2);
System.out.println(list2.size());
for (Person person : list2) {
System.out.println(person);
}
普通字段的map转换成json字符串
Mapmap = new HashMap();
map.put("name","firstname");
map.put("pass","123");
map.put("age",""+34);
Gson gson5 = new Gson();
String jsonStr = gson5.toJson(map);
System.out.println(jsonStr);
将map里存放的对象类型的所有数据转换成json
Mapmap2 = new HashMap();
Person p1 = new Person("张三",55,false,new Address("once","惠州","广东"));
Person p2 = new Person("wang三",55,false,new Address("fotr","噶ung州","广速度"));
map2.put("one",p1);
map2.put("two",p2);
Gson gson6 = new Gson();
String jsonStr2 = gson6.toJson(map2);
System.out.println(jsonStr2);
复杂嵌套的list转换成json数组
Listlist1 = new ArrayList();
list1.add(new Address("once","广东","中国"));
list1.add(new Address("once","广东","中国"));
UserInfo info1 = new UserInfo(11,"张三","10231",list1);
Listlist2 = new ArrayList();
list2.add(new Address("two","山西","中国"));
list2.add(new Address("two","山西","中国"));
UserInfo info2 = new UserInfo(22,"网五","10231",list2);
Listlist3 = new ArrayList();
list3.add(info1);
list3.add(info2);
Gson gson6 = new Gson();
Type userListType = new TypeToken>() {}.getType();
String jsonStr = gson6.toJson(list3, userListType);
System.out.println(jsonStr);