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 不行的!我这个就是重复的报那个错
2 年多之前 回复
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币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!
其他相关推荐
C/C++,Java,PHP,JavaScript,Json数组、对象赋值时,最后一个元素后面是否可以带逗号?
1 C,C++,Java,PHP都能容忍末尾的逗号C,C++,Java中对数组赋值时,最后一个元素末尾的逗号可有可无。下面两行代码对这些语言来说是等效的。int a[] = {1,2,3}; /* 正确 */ int a[] = {1,2,3,}; /* 正确 */PHP这一点也继承了C的特点,下面的两行代码等效。$a = array(1,2,3); /* 正确 */ $a = a
解析json对象包含多个数组
{   &quot;JobSalarys&quot;:[     {       &quot;Salary&quot;:&quot;10000-15000&quot;,       &quot;Location_L&quot;:&quot;北京&quot;,       &quot;Location_P&quot;:&quot;21.93%&quot;,       &quot;Education_L&quot;:&quot;本科及以上&quot;,       &quot;Education_P&quot
js获取返回jsonArray中最后一个对象的id
//js获取返回jsonArray中最后一个对象的id maxId=eval(jsonArray数组)[eval(jsonArray数组).length-1]["id"];
Json解析,同一个字段,获取的可能是对象,数组,或是null
Json解析,同一个字段,获取的可能是对象,数组,或是null的解决:比如 “sights”字段大多数情况下是JSONObject,但是当”sights”字段没有数据时,后台直接返回null值 ,而null值是不属于JSONObject的,所以在此我们用get();方法获取字段,并判断该字段的类型 JSONObject sights = travelDetail.getJSONObject("s
一个数组中有多个对象
一个数组中有多个对象,每个对象中又有数组 var ps = [               {                   teacherName:'1',                   students:['111','222']               },               {                   teacherName:'2',  
Json文档中有多个属性,其中一个属性包含一个数组的解析
1.首先自定义一个Json文本文件 { "Name":"cc", "Level":8, "Age":18, "SkillList":[   {     "Id": 3,     "Name": "天下无贼",     "Damage": 123   },   {     "Id": 4,     "Name": "空格",     "Damage": 150   },
从JsonArray中取出最后三个对象组成新的JsonArray
JSONArray advice为: "[{name:'a',value:'aa'},{name:'b',value:'bb'},{name:'c',value:'cc'},{name:'d',value:'dd'}]" 通过以下代码可以取出最后三项,并组成新的JSONArray advice_3:JSONArray advice = data.optJSONArray("doctorAdvice"
关于json数组拼接中对象的对象具体实现
就直接上代码了   创建二级对象的原因是后台数据是多张表传到前端显示的,为了方便传输,采用这样的拼接。 var categoryName=$('#categoryName option:checked').text(); var infoId=$('#numberID').val(); var infoName=$('#infoName').val(); var cityCode=$('#
json:创建一个对象,其它一个属性是数组;创建一个对象,使用function作为属性值
创建一个对象,其它一个属性是数组 var p = { id:1, name:'哈哈', home:['湘潭','长沙','广州'] }; for(var i=0;i<p.home.length;i++){ document.write(p.home[i]+" "); }
关于java中数组也是对象的强悍解释
Object[]  objs = new Object[3];Object obj = objs;objs = (Object[])obj;另外,下文对java中的数组的本质做了一些探讨:http://dev.csdn.net/author/DeepNightTwo/afb7e220bdf5423ba656f84b6a183b44.html另,/**//* * TestObje