(三)解析Json数组(多条Json数据)
比如有如下Json数据:
[{'name':'John', 'grade':[{'course':'English','score':100},{'course':'Math','score':78}]}, //注:Json数组最外层一定要加"[]"
{'name':'Tom', 'grade':[{'course':'English','score':86},{'course':'Math','score':90}]}]
如何处理这样的数据呢?就要用到List。步骤如下:
示例程序如下:
import java.lang.reflect.Type;
import java.util.List; import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken; /**
* 用GSON解析Json数组
*/
public class GsonTest {
public static void main(String[] args) {
// Json数组最外层要加"[]"
String jsonData = "[{'name':'John', 'grade':[{'course':'English','score':100},{'course':'Math','score':78}]},{'name':'Tom', 'grade':[{'course':'English','score':86},{'course':'Math','score':90}]}]"; List<Student> students = GsonUtil.parseJsonArrayWithGson(jsonData,
Student.class);
System.out.println(students);
}
} /*
* 封装的GSON解析工具类,提供泛型参数
*/
class GsonUtil {
// 将Json数据解析成相应的映射对象
public static <T> T parseJsonWithGson(String jsonData, Class<T> type) {
Gson gson = new Gson();
T result = gson.fromJson(jsonData, type);
return result;
} // 将Json数组解析成相应的映射对象列表
public static <T> List<T> parseJsonArrayWithGson(String jsonData,
Class<T> type) {
Gson gson = new Gson();
List<T> result = gson.fromJson(jsonData, new TypeToken<List<T>>() {
}.getType());
return result;
}
} class Student {
private String name;
private List<Grade> grade; // 因为grade是个数组,所以要定义成List public class Grade {
private String course;
private String score; public String getCourse() {
return course;
} public void setCourse(String course) {
this.course = course;
} public String getScore() {
return score;
} public void setScore(String score) {
this.score = score;
} } public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public List<Grade> getGrade() {
return grade;
} public void setGrade(List<Grade> grade) {
this.grade = grade;
}
}
输出结果:
[{name=John, grade=[{course=English, score=100.0}, {course=Math, score=78.0}]}, {name=Tom, grade=[{course=English, score=86.0}, {course=Math, score=90.0}]}]