使用 google gson,它非常简单,可以完美地处理数组和列表
public class CollectionToJson {
public static void main(String[] args) {
//
// Converts a collection of string object into JSON string.
//
List<String> names = new ArrayList<String>();
names.add("Alice");
names.add("Bob");
names.add("Carol");
names.add("Mallory");
Gson gson = new Gson();
String jsonNames = gson.toJson(names);
System.out.println("jsonNames = " + jsonNames);
//
// Converts a collection Student object into JSON string
//
Student a = new Student("Alice", "Apple St",
CollectionToJson.getDOB(2000, 10, 1));
Student b = new Student("Bob", "Banana St", null);
Student c = new Student("Carol", "Grape St",
CollectionToJson.getDOB(2000, 5, 21));
Student d = new Student("Mallory", "Mango St", null);
List<Student> students = new ArrayList<Student>();
students.add(a);
students.add(b);
students.add(c);
students.add(d);
gson = new Gson();
String jsonStudents = gson.toJson(students);
System.out.println("jsonStudents = " + jsonStudents);
//
// Converts JSON string into a collection of Student object.
//
Type type = new TypeToken<List<Student>>(){}.getType();
List<Student> studentList = gson.fromJson(jsonStudents, type);
for (Student student : studentList) {
System.out.println("student.getName() = " + student.getName());
}
}
private static Date getDOB(int year, int month, int date) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month - 1);
calendar.set(Calendar.DATE, date);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return calendar.getTime();
}
}
输出
jsonNames = ["Alice","Bob","Carol","Mallory"]
jsonStudents = [{"name":"Alice","address":"Apple St","dateOfBirth":"Nov 1, 3900 12:00:00 AM"},{"name":"Bob","address":"Banana St"},{"name":"Carol","address":"Grape St","dateOfBirth":"Jun 21, 3900 12:00:00 AM"},{"name":"Mallory","address":"Mango St"}]
student.getName() = Alice
student.getName() = Bob
student.getName() = Carol
student.getName() = Mallory