用原生操作json的方法过于复杂,所以fastJSON库提供了更为简单的方法
1 需要在项目中引入fastjson的jar包或者直接在gradle中引入fastjson
2 fastjson中的提供了多种静态方法,用JSON.方法名()进行调用
根据json字符串创建实体类
实际开发中json字符串都要转化成相应的实体类然后再进行操作的,所以需要首先根据json字符串创建相应的实体类
[]对应list
{}对应实体类
嵌套的json就需要创建多个实体类
json格式如下
{
"id": 1,
"name": "Teacher 1",
"students": [
{
"age": 18,
"id": 0,
"name": "Student0"
},
{
"age": 19,
"id": 1,
"name": "Student1"
}
]
}```
创建对应的两个实体类
最大的类是一个Teacher类,它里面还有一个JSONObject的对象对应学生类,所以还需要创建一个学生类,Student类的对象是Teacher类的成员变量,而且是一个LIst类型的成员变量
注意事项
1 实体类中成员变量的名称必须和json中的key完全相同
例如Teacher类中的id,name,students
Student类中的age,id,name
实体类的类名可以自定义,无所谓
2 每个实体类必须都要有一个无参的空构造函数
上面的json对应的实体类如下
Teacher类
public class Teacher {
private int id;
private String name;
private List<Student> students;
public Teacher(){}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
@Override
public String toString() {
return "Teacher{" +
"id=" + id +
", name='" + name + '\'' +
", students=" + students +
'}';
}
}
Student类
public class Student {
private int id;
private String name;
private int age;
public Student(){}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name=" + name +
", age=" + age +
'}';
}
}
######解析
1 JSON.parse(String json)
传入一个json字符串,返回JSONObject或者JSONArray对象
如果json字符串是jsonObject类型的就返回JSONObject对象
如果json字符串是jsonArray类型的就返回JSONArray对象
但是需要类型转换
JSONObject object= (JSONObject) JSON.parse(json);
JSONArray array=(JSONArray)JSON.parse(json1);
2 JSON.parseObject(String json)
将json字符串转化为JSONObject对象
JSONObject object= JSON.parseObject(json);
3 JSON.parseArray(String json)
将json字符串转化为JSONArray对象
JSONArray array=JSON.parseArray(json1);
4 parseObject(String text, Class<T> clazz);
传入jsonObject类型的json字符串,和对应的实体类的类名.class,直接将json字符串转化为实体类
以最开始给出的字符串和实体类为例
Teacher teacher=JSON.parseObject(json,Teacher.class);
一行代码搞定,调用teacher中的get方法就可以获得对应的数据。
5 parseArray(String text, Class<T> clazz);
如果是一个jsonArray类型的json字符串,里面的每一个就可以按照里面的jsonObject类型的字符串建立实体类,然后用parseArray方法获取实体类列表
例如是这样一组格式的json
[
{
"id": 1,
"name": "Teacher 1",
"students": [
{
"age": 18,
"id": 0,
"name": "Student0"
},
{
"age": 19,
"id": 1,
"name": "Student1"
}
]
},
{
"id": 2,
"name": "Teacher 2",
"students": [
{
"age": 18,
"id":3,
"name": "Student3"
},
{
"age": 19,
"id":4,
"name": "Student4"
}
]
}
]
对应的实体类还是Teacher类
List<Teacher> teacher=JSON.parseArray(json,Teacher.class);
这样便得到了实体类的list对象
######构造json字符串
1 JSON.toJSONString(Object o)
可以传入实体类对象,或者传入实体类的list,得到相应的json字符串
传入实体类对象
//创建一个Teacher类的对象,并赋值
Teacher teacher=new Teacher();
teacher.setName("teacher"+1);
teacher.setId(1);
List<Student> students=new ArrayList<>();
for(int i=0;i<3;i++)
{
Student student=new Student();
student.setAge(i);
student.setId(i);
student.setName("aaa"+i);
}
teacher.setStudents(students);
//转化成json字符串
String jsonString= JSON.toJSONString(teacher);
Log.i("test",jsonString);
{"id":1,"name":"teacher1","students":[{"age":0,"id":0,"name":"aaa0"},{"age":1,"id":1,"name":"aaa1"},{"age":2,"id":2,"name":"aaa2"}]}
传入实体类list
//创建Teacher的list
List<Teacher> teachers=new ArrayList<>();
for(int j=0;j<4;j++)
{
//创建一个Teacher类的对象,并赋值
Teacher teacher=new Teacher();
teacher.setName("teacher"+j);
teacher.setId(1);
List<Student> students=new ArrayList<>();
for(int i=0;i<3;i++)
{
Student student=new Student();
student.setAge(i);
student.setId(i);
student.setName("aaa"+i);
students.add(student);
}
teacher.setStudents(students);
teachers.add(teacher);
}
//转化成json字符串
String jsonString= JSON.toJSONString(teachers);
[{"id":1,"name":"teacher0","students":[{"age":0,"id":0,"name":"aaa0"},
{"age":1,"id":1,"name":"aaa1"},{"age":2,"id":2,"name":"aaa2"}]},
{"id":1,"name":"teacher1","students":[{"age":0,"id":0,"name":"aaa0"},
{"age":1,"id":1,"name":"aaa1"},{"age":2,"id":2,"name":"aaa2"}]},
{"id":1,"name":"teacher2","students":[{"age":0,"id":0,"name":"aaa0"},
{"age":1,"id":1,"name":"aaa1"},{"age":2,"id":2,"name":"aaa2"}]},
{"id":1,"name":"teacher3","students":[{"age":0,"id":0,"name":"aaa0"},
{"age":1,"id":1,"name":"aaa1"},{"age":2,"id":2,"name":"aaa2"}]}]
2 JSON.toJSONString(Object o, boolean prettyFormat)
和上面的相比多了一个参数,代表是否要讲字符串转化成json标准格式,如果传入false。就和上面一样,如果传入true,就是下面这样的,但是实质上没有区别,只是展示上美观与否的问题
传入实体类
{
"id": 1,
"name": "teacher1",
"students": [
{
"age": 0,
"id": 0,
"name": "aaa0"
},
{
"age": 1,
"id": 1,
"name": "aaa1"
},
{
"age": 2,
"id": 2,
"name": "aaa2"
}
]
}```
传入实体类list
就不贴了
最后一个方法
JSON.toJSON(Object o)
该方法将实体类或者实体类list转化成JSONObject或者JSONArray对象
转化为JSONObject,其中teacher是一个实体类对象
JSONArray array=(JSONArray) JSON.toJSON(teachers);
转化为JSONArray,其中teachers是一个实体类list
···
JSONObject object=(JSONObject) JSON.toJSON(teacher);
···
这个方法感觉实际用处不是很大