android jackson 解析json字符串,android:json解析的两个工具:Gson和Jackson的使用小样例...
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: [email?protected]
*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: [email?protected]
*2014-5-12 Nanjing,njupt,China
*/
public class School {
private String name;
private String address;
List 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 getStudents() {
return students;
}
public void setStudents(List students) {
this.students = students;
}
}
初始化一個School的java對象,待使用:
private School initSchoolData(){
School school = new School();
school.setAddress("Nanjing");
school.setName("NUPT");
List list = new ArrayList();
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有非常多種參數類型,適用于多中場合。
注意:以上僅僅是這兩個工具類的基本使用方法,很多其它了解。參見官方文檔。
總結
以上是生活随笔為你收集整理的android jackson 解析json字符串,android:json解析的两个工具:Gson和Jackson的使用小样例...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 各种说明方法的答题格式_初中语文阅读答题
- 下一篇: JSON对象转化为JSON字符串