JSONObject转换成对应的需求的目标ITeye - 牛牛娱乐

JSONObject转换成对应的需求的目标ITeye

2019-01-10 20:40:50 | 作者: 忆香 | 标签: 学生,编号,教师 | 浏览: 1586

在运用JSONObject.toBean的时分,得到的Bean里边的杂乱数据类型( 假如转化的类中有调集)不能转化成需求的目标类型。

 java.lang.ClassCastException: net.sf.ezmorph.bean.MorphDynaBean cannot be cast to ......

package com.edu.xukai;
 * @author xukai
public class Student {
 private String stuNo;
 private String stuName;
 public Student() {
 public Student(String stuNo, String stuName) {
 this.stuNo = stuNo;
 this.stuName = stuName;
 // getter setter
 @Override
 public String toString() {
 return "Student [stuNo=" + stuNo + ", stuName=" + stuName + "]";

 

 

package com.edu.xukai;
import java.util.List;
 * @author xukai
public class Teacher {
 private String teaId;
 private String teaName;
 private List Student stus;
 public Teacher() {
 public Teacher(String teaId, String teaName, List Student stus) {
 this.teaId = teaId;
 this.teaName = teaName;
 this.stus = stus;
//getter setter
}
package com.edu.xukai;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;
 * @author xukai
public class TestJSONObject {
 public static void main(String[] args) {
 Student student_1 = new Student("学号1", "学生1");
 List Student stus = new ArrayList Student 
 stus.add(student_1);
 Teacher teacher_1 = new Teacher("编号1", "教师1", stus);
 JSONObject obj = JSONObject.fromObject(teacher_1);
 System.out.println("JSON格局的Teacher- " + obj.toString());
 Teacher teacherBean = (Teacher) JSONObject.toBean(obj, Teacher.class);
 try {
 Student studentBean = teacherBean.getStus().get(0);
 System.out.println(studentBean);
 } catch (Exception e) {
 System.out.println("出现异常");
 e.printStackTrace();

 运转能够看到控制台打印成果:

 

JSON格局的Teacher- {"stus":[{"stuName":"学生1","stuNo":"学号1"}],"teaId":"编号1","teaName":"教师1"}
出现异常
java.lang.ClassCastException: net.sf.ezmorph.bean.MorphDynaBean cannot be cast to com.edu.xukai.Student
at com.edu.xukai.TestJSONObject.main(TestJSONObject.java:25)

解决办法:运用JSONObject.toBean(jsonObject, beanClass, classMap)

package com.edu.xukai;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;
 * @author xukai
public class TestJSONObject { 
 public static void main(String[] args) {
 Student student_1 = new Student("学号1", "学生1");
 List Student stus = new ArrayList Student 
 stus.add(student_1);
 Teacher teacher_1 = new Teacher("编号1", "教师1", stus);
 JSONObject obj = JSONObject.fromObject(teacher_1);
 System.out.println("JSON格局的Teacher- " + obj.toString());
 // 界说一个Map
 Map String, Class Student map = new HashMap String, Class Student ();
 map.put("stus", Student.class); // key为teacher私有变量的特点名
 // 运用JSONObject.toBean(jsonObject, beanClass, classMap)
 Teacher teacherBean = (Teacher) JSONObject.toBean(obj, Teacher.class, map);
 try {
 Student studentBean = teacherBean.getStus().get(0);
 System.out.println(studentBean);
 } catch (Exception e) {
 System.out.println("出现异常");
 e.printStackTrace();

   控制台打印成果:

JSON格局的Teacher- {"stus":[{"stuName":"学生1","stuNo":"学号1"}],"teaId":"编号1","teaName":"教师1"}
Student [stuNo=学号1, stuName=学生1]

 

 

 

版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表牛牛娱乐立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章

阅读排行

  • 1

    java多线程(七)ITeye

    线程,倾向,目标
  • 2

    java线程池ITeye

    线程,使命,工人
  • 3
  • 4
  • 5

    修饰符ITeye

    润饰,能够,直接
  • 6
  • 7
  • 8

    第02章 根底中心ITeye

    目标,根底,中心
  • 9
  • 10