1 package per.swwfourteen.fourteen; 2 3 public class Student { 4 private String id; 5 6 private String name; 7 8 private String password; 9 10 private String age; 11 12 public void setId(String id) { 13 this.id = id; 14 } 15 16 public String getId() { 17 return this.id; 18 } 19 20 public void setName(String name) { 21 this.name = name; 22 } 23 24 public String getName() { 25 return this.name; 26 } 27 28 public void setPassword(String password) { 29 this.password = password; 30 } 31 32 public String getPassword() { 33 return this.password; 34 } 35 36 public void setAge(String age) { 37 this.age = age; 38 } 39 40 public String getAge() { 41 return this.age; 42 } 43 44 }
1 package per.swwfourteen.fourteen; 2 3 public class Teacher { 4 private String id; 5 6 private String name; 7 8 private String password; 9 10 private String age; 11 12 private Car car; 13 14 public void setId(String id) { 15 this.id = id; 16 } 17 18 public String getId() { 19 return this.id; 20 } 21 22 public void setName(String name) { 23 this.name = name; 24 } 25 26 public String getName() { 27 return this.name; 28 } 29 30 public void setPassword(String password) { 31 this.password = password; 32 } 33 34 public String getPassword() { 35 return this.password; 36 } 37 38 public void setAge(String age) { 39 this.age = age; 40 } 41 42 public String getAge() { 43 return this.age; 44 } 45 46 public void setCar(Car car) { 47 this.car = car; 48 } 49 50 public Car getCar() { 51 return this.car; 52 } 53 54 }
1 package per.swwfourteen.fourteen; 2 3 public class Car { 4 private String num; 5 6 public void setNum(String num) { 7 this.num = num; 8 } 9 10 public String getNum() { 11 return this.num; 12 } 13 14 }
1 package per.swwfourteen.fourteen; 2 3 import java.util.List; 4 5 public class Root_List { 6 private List<Student> student; 7 8 private List<Teacher> teacher; 9 10 public void setStudent(List<Student> student) { 11 this.student = student; 12 } 13 14 public List<Student> getStudent() { 15 return this.student; 16 } 17 18 public void setTeacher(List<Teacher> teacher) { 19 this.teacher = teacher; 20 } 21 22 public List<Teacher> getTeacher() { 23 return this.teacher; 24 } 25 26 }
1 package per.swwfourteen.fourteen; 2 3 import java.io.BufferedReader; 4 import java.io.File; 5 import java.io.FileReader; 6 import java.io.IOException; 7 8 import com.alibaba.fastjson.JSONObject; 9 10 public class JsonRoot { 11 public static void main(String[] args)throws Exception{ 12 Root_List root=JSONObject.parseObject(ReadRoot("D:"+File.separator+"1.json"),Root_List.class); 13 for(Student s:root.getStudent()){ 14 System.out.println(s.getId()); 15 System.out.println(s.getName()); 16 System.out.println(s.getAge()); 17 System.out.println(s.getPassword()); 18 } 19 for(Teacher t:root.getTeacher()){ 20 System.out.println(t.getId()); 21 System.out.println(t.getName()); 22 System.out.println(t.getAge()); 23 System.out.println(t.getPassword()); 24 System.out.println(t.getCar()); 25 } 26 } 27 28 public static String ReadRoot(String rer)throws IOException{ 29 StringBuffer sb=new StringBuffer(); 30 BufferedReader br=new BufferedReader(new FileReader(rer)); 31 char[] it=new char[1024]; 32 int sum=0; 33 while((sum=br.read(it))!=-1){ 34 String s=String.valueOf(it, 0, sum); 35 sb.append(s); 36 } 37 br.close(); 38 return sb.toString(); 39 } 40 }
运行后提示13行报错,
Exception in thread "main" java.lang.NullPointerException
at per.swwfourteen.fourteen.JsonRoot.main(JsonRoot.java:13)
1 { 2 "Root": { 3 "student": [ 4 { 5 "id": "001", 6 "name": "student1", 7 "password": "123", 8 "age": "20" 9 }, 10 { 11 "id": "002", 12 "name": "student2", 13 "password": "456", 14 "age": "21" 15 }, 16 { 17 "id": "003", 18 "name": "student3", 19 "password": "123", 20 "age": "21" 21 } 22 ], 23 "teacher": [ 24 { 25 "id": "001", 26 "name": "teacher1", 27 "password": "123", 28 "age": "20", 29 "car": { "num": "098" } 30 }, 31 { 32 "id": "002", 33 "name": "teacher2", 34 "password": "123", 35 "age": "20", 36 "car": { "num": "098" } 37 } 38 ] 39 } 40 }
json转成对象后,root是个map类吧?你用root.get("student")试试,或者遍历keySet集合
是List
root. 只能点出getClass,和getStudentList以及getTeacherList
@略知闻风雨: for student的循环注释掉,teacher循环没问题吗
@Laughing_Lz: 有= =,一模一样
@略知闻风雨: 把Root_List root=JSONObject.parseObject(ReadRoot("D:"+File.separator+"1.json"),Root_List.class);
改成 Map root = JSONObject.parseObject(ReadRoot("D:"+File.separator+"1.json"),Root_List.class);
然后试试root.get("student");
@略知闻风雨: 给你跑了一遍才看出来。你的json数据错了,你带个Root有什么用,删掉它和它的{},再跑就OK了
@略知闻风雨:
1 { 2 "student": [ 3 { 4 "id": "001", 5 "name": "student1", 6 "password": "123", 7 "age": "20" 8 }, 9 { 10 "id": "002", 11 "name": "student2", 12 "password": "456", 13 "age": "21" 14 }, 15 { 16 "id": "003", 17 "name": "student3", 18 "password": "123", 19 "age": "21" 20 } 21 ], 22 "teacher": [ 23 { 24 "id": "001", 25 "name": "teacher1", 26 "password": "123", 27 "age": "20", 28 "car": { "num": "098" } 29 }, 30 { 31 "id": "002", 32 "name": "teacher2", 33 "password": "123", 34 "age": "20", 35 "car": { "num": "098" } 36 } 37 ] 38 39 }
@Laughing_Lz: ....我删了还报= =
@略知闻风雨:
1 Gson gson = new Gson(); 2 Root_List root = gson.fromJson(ReadRoot("D:"+File.separator+"1.json"), Root_List.class);
换掉alibaba的吧···我这用的Gson没问题
@略知闻风雨: json转对象别用alibba的包,换gson,这个http://download.csdn.net/detail/zxyyxzzxy004/8206489
@Laughing_Lz: 这个没用过- -
@Laughing_Lz: 去掉大括号弄好了,太坑了做了一个别的项目它自己就好了
public List<Student> getStudent() { if(this.student==null)return new List<Student>(); return this.student; }
new List<Student>();
这块儿报错..
@略知闻风雨: .我又不会写Java代码..只是说思路.
问题是因为你对null 循环了.只要返回一个空数组出去就好了.
空对象,你调试一下,root中没对象,读json可能有问题
d一个for循环都进不去
@略知闻风雨: 肯定啊,空对象,就是你的root中集合为空,取不到student对象,我是让你看第十二行
Root_List root=JSONObject.parseObject(ReadRoot("D:"+File.separator+"1.json"),Root_List.class);
这里面的root中有数据没,应该是没有,解析json获得数据为空
,你看下是不是
@博客园乄小光: 是的,student和teacher都是null
@略知闻风雨: 还是读json,你看一下路径,读json的逻辑是不是有问题,往上一步调试,进到ReadRoot方法里面,一步一步找
@博客园乄小光: 纳闷了,我student类里是有东西的啊,怎么就null了呢
@略知闻风雨: 跟你student类没关系,要不就是路径错了,要不就是读json逻辑错了,读不到数据,就为空
@略知闻风雨: 我现在有任务,要不然帮你把代码弄出来了
@略知闻风雨: 不要方,仔细点调试,一行一行观察读的数据,是不是读出来了,是不是处理对了
@博客园乄小光: 方的不行,这个是我自己练手的,不着急,我慢慢找抽空帮我感激不尽,读到Root_List root=JSONObject.parseObject(ReadRoot("D:"+File.separator+"1.json"),Root_List.class);
发现student和teacher获取到的都是null;然后for循环就不用想了,感觉应该是ReadRoot的问题,但是ReadRoot运行完无异常- -
@略知闻风雨: 无异常和无数据不冲突,语法没问题肯定无异常。。。。。。你把ReadRoot里面代码全删了也无异常。。。。,还是慢慢弄读json的代码
@博客园乄小光:
{ "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" } } ] }
为什么去掉大括号就好了呢?Root不是在xml中是根节点么?难道在json里就不要了?
@略知闻风雨: 。。。
建议在可能为空的情况下,一定要做为空判断。一般合格的java代码都应该这样处理
现在读不到数据呀