JSONObject转换成对应的需要的对象

在使用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]

 

 

猜你喜欢

转载自dengqw.iteye.com/blog/2396691
今日推荐