2 qq 15173955 qq_15173955 于 2016.04.14 10:48 提问

json中一个对象里面有两个数组为什么数组最后是null?

话不多说直接上代码

 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" }
      }
    ]
  }
}

6个回答

DarrenMJ
DarrenMJ   2016.04.14 11:12

我昨天也遇到这种问题了,后台定义的List是private的,到前台就是undefined,我最后是在没办法就定义成public了,结果就好了,你试试看行不?

qq_15173955
qq_15173955 不行的!我这个就是重复的报那个错
一年多之前 回复
cyxevil
cyxevil   2016.04.15 17:26

teacher": [
{
"id": "001",
"name": "teacher1",
"password": "123",
"age": "20",
"car": { "num": "098" }
},
{
"id": "002",
"name": "teacher2",
"password": "123",
"age": "20",
"car": { "num": "098" }
}
]

    这个最外围是List
    然后才是对象,我觉得可能跟这个有关
cyxevil
cyxevil   2016.04.15 17:31

package ;
import java.util.List;
public class Root {
private List student ;

private List teacher ;

public void setStudent(List student){
this.student = student;
}
public List getStudent(){
return this.student;
}
public void setTeacher(List teacher){
this.teacher = teacher;
}
public List getTeacher(){
return this.teacher;
}

}


```bejson生成的规范类,最外面的类是这样的

cyxevil
cyxevil   2016.04.15 17:32

发错了,是这个

```package ;
import java.util.List;
public class Root {
private List student ;

private List teacher ;

public void setStudent(List student){
this.student = student;
}
public List getStudent(){
return this.student;
}
public void setTeacher(List teacher){
this.teacher = teacher;
}
public List getTeacher(){
return this.teacher;
}

}


cyxevil
cyxevil   2016.04.15 17:33

package ;
import java.util.List;
public class Root {
private List student ;

private List teacher ;

public void setStudent(List student){
this.student = student;
}
public List getStudent(){
return this.student;
}
public void setTeacher(List teacher){
this.teacher = teacher;
}
public List getTeacher(){
return this.teacher;
}

}

cyxevil
cyxevil   2016.04.15 17:34

我突然觉得你要自己去试试,capy的时候,集合类型会消失http://www.bejson.com/json2javapojo/

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!