1.简单介绍
json是android与server通信过程中经常使用的数据格式,比如。例如以下是一个json格式的字符串:
{"address":"Nanjing","name":"NUPT","students":[{"name":"stu1","id":"10000","age":20},{"name":"stu1","id":"10001","age":21},{"name":"stu1","id":"10002","age":22}]}
2.android中经常使用的两个json库为:Gson和Jackson,一下就看一下它们是怎样将对象转化为json字符串,以及怎样将json字符串转化为java对象。
定义Student类:
/* * $filename: Student.java,v $ * $Date: 2014-5-12 $ * Copyright (C) ZhengHaibo, Inc. All rights reserved. * This software is Made by Zhenghaibo. */ package com.example.testjson; /* *@author: ZhengHaibo *web: http://blog.csdn.net/nuptboyzhb *mail: zhb931706659@126.com *2014-5-12 Nanjing,njupt,China */ public class Student { private String name; private String id; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
定义School类:
/* * $filename: School.java,v $ * $Date: 2014-5-12 $ * Copyright (C) ZhengHaibo, Inc. All rights reserved. * This software is Made by Zhenghaibo. */ package com.example.testjson; import java.util.List; /* *@author: ZhengHaibo *web: http://blog.csdn.net/nuptboyzhb *mail: zhb931706659@126.com *2014-5-12 Nanjing,njupt,China */ public class School { private String name; private String address; List<Student> students; 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 List<Student> getStudents() { return students; } public void setStudents(List<Student> students) { this.students = students; } }
初始化一个School的java对象,待使用:
private School initSchoolData(){ School school = new School(); school.setAddress("Nanjing"); school.setName("NUPT"); List<Student> list = new ArrayList<Student>(); for(int i= 0;i<3;i++){ Student student = new Student(); student.setAge(20+i); student.setId("1000"+i); student.setName("stu"+1); list.add(student); } school.setStudents(list); return school; }
3.使用Gson。进行java对象与json字符串之间的相互转换
private void testGson(){ School school = initSchoolData(); Gson gson = new Gson(); //将对象转化为json字符串 String jsonString = gson.toJson(school).toString(); //打印 System.out.println("GSON="+jsonString); /******切割线*****/ //将字符串转化为JSON对象 School resultSchool = gson.fromJson(jsonString, School.class); //打印 System.out.println(resultSchool.getStudents().get(0).getName()); }
可见,Gson是通过toJson和fromJson两个方法进行转换的,使用起来十分方便。
4.使用Jackson
private void testJackson(){ School school = initSchoolData(); ObjectMapper objectMapper = new ObjectMapper(); String jsonString = ""; //将对象转化为Json字符串 try { jsonString = objectMapper.writeValueAsString(school); System.out.println("Jackson="+jsonString); } catch (JsonProcessingException e) { // TODO Auto-generated catch block e.printStackTrace(); } //将json字符串解析成java对象 try { School resultSchool = objectMapper.readValue(jsonString, School.class); //打印 System.out.println(resultSchool.getStudents().get(0).getName()); } catch (JsonParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JsonMappingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
使用Jackson,主要是使用writeValueAsString和readValue这两个方法。此外。Jackson的writeValue和readValue有非常多种參数类型,适用于多中场合。
注意:以上仅仅是这两个工具类的基本使用方法,很多其它了解。參见官方文档。
Gson文档:http://code.google.com/p/google-gson/
Jackson:http://jackson.codehaus.org/