Gson
- 解析json数据数组
Gson gson = new Gson();
List<Person> people = gson.fromJson(jsonData, new TypeToken<List<Person>>(){}.getType());
- 解析json数据
Gson gson = new Gson();
Person person = gson.fromJson(jsonData, Person.class);
3.gson适用的自定义类
- 内部嵌套的类必须是static的,要不然解析会出错;
- 类里面的属性名必须跟Json字段里面的Key是一模一样的;
public class JsonBean {
public String a;
public List<B> b;
public C c;
public static class B {
public String b1;
public String b2;
}
public static class C {
public String c1;
public String c2;
}
}