话不多说直接上代码
package per.swwfourteen.fourteen;
import java.util.List;
public class Root_List {
private List<Student> studentList;
private List<Teacher> teacherList;
public List<Student> getStudentList() {
return studentList;
}
public void setStudentList(List<Student> studentList) {
this.studentList = studentList;
}
public List<Teacher> getTeacherList() {
return teacherList;
}
public void setTeacherList(List<Teacher> teacherList) {
this.teacherList = teacherList;
}
}
Root里面有Student跟Tecaher两个集合
package per.swwfourteen.fourteen;
public class Student {
private String id;
private String name;
private String password;
private String age;
public void setId(String id) {
this.id = id;
}
public String getId() {
return this.id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return this.password;
}
public void setAge(String age) {
this.age = age;
}
public String getAge() {
return this.age;
}
}
Student的getset
package per.swwfourteen.fourteen;
public class Teacher {
private String id;
private String name;
private String password;
private String age;
private Car car;
public void setId(String id) {
this.id = id;
}
public String getId() {
return this.id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return this.password;
}
public void setAge(String age) {
this.age = age;
}
public String getAge() {
return this.age;
}
public void setCar(Car car) {
this.car = car;
}
public Car getCar() {
return this.car;
}
}
Teacher的getset
package per.swwfourteen.fourteen;
public class Car {
private String num;
public void setNum(String num) {
this.num = num;
}
public String getNum() {
return this.num;
}
}
Car
package per.swwfourteen.fourteen;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import com.alibaba.fastjson.JSONObject;
public class JsonRoot {
public static void main(String[] args)throws Exception{
Root_List root=JSONObject.parseObject(ReadRoot("D:"+File.separator+"1.json"),Root_List.class);
for(Student s:root.getStudentList()){
System.out.println(s.getId());
System.out.println(s.getName());
System.out.println(s.getAge());
System.out.println(s.getPassword());
}
for(Teacher t:root.getTeacherList()){
System.out.println(t.getId());
System.out.println(t.getName());
System.out.println(t.getAge());
System.out.println(t.getPassword());
System.out.println(t.getCar());
}
}
public static String ReadRoot(String rer)throws IOException{
StringBuffer sb=new StringBuffer();
BufferedReader br=new BufferedReader(new FileReader(rer));
char[] it=new char[1024];
int sum=0;
while((sum=br.read(it))!=-1){
String s=String.valueOf(it, 0, sum);
sb.append(s);
}
br.close();
return sb.toString();
}
}
然后一运行报错
Exception in thread "main" java.lang.NullPointerException
at per.swwfourteen.fourteen.JsonRoot.main(JsonRoot.java:12)
调试发现在运行时
Root_List root=JSONObject.parseObject(ReadRoot("D:"+File.separator+"1.json"),Root_List.class);
其中student和teacher的值都是null,很是不解,求大神解惑,json在下面
{
"Root": {
"student": [
{
"id": "001",
"name": "student1",
"password": "123",
"age": "20"
},
{
"id": "002",
"name": "student2",
"password": "456",
"age": "21"
},
{
"id": "003",
"name": "student3",
"password": "123",
"age": "21"
}
],
"teacher": [
{
"id": "001",
"name": "teacher1",
"password": "123",
"age": "20",
"car": { "num": "098" }
},
{
"id": "002",
"name": "teacher2",
"password": "123",
"age": "20",
"car": { "num": "098" }
}
]
}
}