JSON转对象关键语法:gson.fromJson
对象转JSON关键语法:gson.toJson
================================================================================================================
1、第一种,比如json字符串为:[{"name":"name0","age":0}]
代码:
Person person = gson.fromJson(str, Person.class);
================================================================================================================
2、第二种,转换成列表类型:[{"name":"name0","age":0},{"name":"name1","age":5}]
代码:
List<Person> ps = gson.fromJson(str, new TypeToken<List<Person>>(){}.getType());
for(int i = 0; i < ps.size() ; i++)
{
Person p = ps.get(i);
System.out.println(p.toString());
}
可以看到上面的代码使用了TypeToken,它是gson提供的数据类型转换器,可以支持各种数据集合类型转换。
================================================================================================================
Convert numbers array into JSON string.(number数组转json字符串)
int[] numbers = {1, 1, 2, 3, 5, 8, 13};
Gson gson = new Gson();
String numbersJson = gson.toJson(numbers);
System.out.println("numbersJson = " + numbersJson);
================================================================================================================
Convert strings array into JSON string(string数组转json字符串)
String[] days = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
String daysJson = gson.toJson(days);
System.out.println("daysJson = " + daysJson);
================================================================================================================
Convert from JSON string to a primitive array of int.(从JSON字符串转换为原始数组int。)
int[] fibonacci = gson.fromJson(numbersJson, int[].class);
for (int i = 0; i < fibonacci.length; i++) {
System.out.print(fibonacci[i] + " ");
}
System.out.println("");
================================================================================================================
Convert from JSON string to a string array.(从JSON字符串转换为一个字符串数组。)
String[] weekDays = gson.fromJson(daysJson, String[].class);
for (int i = 0; i < weekDays.length; i++) {
System.out.print(weekDays[i] + " ");
}
System.out.println("");
================================================================================================================
Converting multidimensional array into JSON(多维数组转换成JSON)
int[][] data = {{1, 2, 3}, {3, 4, 5}, {4, 5, 6}};
String json = gson.toJson(data);
System.out.println("Data = " + json);
//
// Convert JSON string into multidimensional array of int.
//
int[][] dataMap = gson.fromJson(json, int[][].class);
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
System.out.print(data[i][j] + " ");
}
System.out.println("");
}
================================================================================================================
Converts a collection of string object into JSON string.(转换成JSON字符串集的字符串对象。)
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(将收集的学生对象转换成JSON字符串)
Student a = new Student("Alice", "Apple St", new Date(2000, 10, 1));
Student b = new Student("Bob", "Banana St", null);
Student c = new Student("Carol", "Grape St", new Date(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 gson = new Gson();
String jsonStudents = gson.toJson(students);
System.out.println("jsonStudents = " + jsonStudents);
================================================================================================================
Converts JSON string into a collection of Student object.(将JSON字符串为一个Student对象。)
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());
}
================================================================================================================
json 串转化成 Java对象。()
String json = "{\"name\":\"Duke\",\"address\":\"Menlo Park\",\"dateOfBirth\":\"Feb 1, 2000 12:00:00 AM\"}";
Gson gson = new Gson();
Student student = gson.fromJson(json, Student.class);
System.out.println("student.getName() = " + student.getName());
System.out.println("student.getAddress() = " + student.getAddress());
System.out.println("student.getDateOfBirth() = " + student.getDateOfBirth());
================================================================================================================
Gson处理一个对象的某一字段。()
Calendar dob = Calendar.getInstance();
dob.set(1980, 10, 11);
People people = new People("John", "350 Banana St.", dob.getTime());
people.setSecret("This is a secret!");
Gson gson = new Gson();
String json = gson.toJson(people);
System.out.println("json = " + json);
输出结果:json = {"name":"John","address":"350 Banana St.","dateOfBirth":"Nov 11, 1980 8:47:04 AM"}
实体类
public class People {
private String name;
private String address;
private Date dateOfBirth;
private Integer age;
private transient String secret;
public People(String name, String address, Date dateOfBirth) {
this.name = name;
this.address = address;
this.dateOfBirth = dateOfBirth;
}
public String getSecret() {
return secret;
}
public void setSecret(String secret) {
this.secret = secret;
}
}
Android Gson解析
最新推荐文章于 2021-11-24 21:35:02 发布