看完第一行代码之后,跟着书写了酷欧天气的例子,但是由于中国天气网的接口已于15年停止使用,因此虽然跟着书写完了小例子,但是运行之后仅限于读取到省份信息,无法体会到所有内容。
因此我想为我的第一个小例子画上圆满的句号,在百度API上找到了免费的天气接口,可是接口请求得到的内容与书中原本的接口完全不一样,需要改动的地方比较多,因此我便先去了解了接口请求之后得到的json数据如何解析。
文章参考地址1 《 android:json解析的两个工具:Gson和Jackson的使用小例子》
第一篇文章中通过两个简单的例子来讲述Gson与Jackson这两个封装好的Json工具类基本使用方法。
{"address":"Nanjing","name":"NUPT","students":[{"name":"stu1","id":"10000","age":20},{"name":"stu1","id":"10001","age":21},{"name":"stu1","id":"10002","age":22}]}
步骤:
1.映射实体类,定义Student类
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类:
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;
}
}
2.初始化一个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,将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());
}
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();
}
}
依照着这个小例子,我用Gson把天气数据转化成了Java对象。
第二篇文章由浅入深的讲述了Gson解析原理与过程,涉及简单Json数据与复杂的嵌套Json数据的序列化与反序列化。
原文链接: javacreed 翻译: ImportNew.com - 飘扬叶
译文链接: http://www.importnew.com/16638.html