Convert JSONObject to the corresponding required object

When using JSONObject.toBean, the complex data type in the obtained Bean (if there is a collection in the converted class) cannot be converted into the required object type.

 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("Number 1", "Teacher 1", stus);
		JSONObject obj = JSONObject.fromObject(teacher_1);
		System.out.println("Teacher->" in JSON format + 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("Exception occurred");
			e.printStackTrace ();
		}
	}
}

 You can see the console print results by running:

 

Teacher->{"stus":[{"stuName":"Student 1","stuNo":"Student ID 1"}],"teaId":"Number 1","teaName":"Teacher 1 in JSON format "}
Exception
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)

Solution: use 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("Number 1", "Teacher 1", stus);
		JSONObject obj = JSONObject.fromObject(teacher_1);
		System.out.println("Teacher->" in JSON format + obj.toString());
		
		// define a Map
		Map<String, Class<Student>> map = new HashMap<String, Class<Student>>();
		map.put("stus", Student.class); // key is the attribute name of teacher's private variable
		// 使用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("Exception occurred");
			e.printStackTrace ();
		}
	}	
}

   The console prints the result:

Teacher->{"stus":[{"stuName":"Student 1","stuNo":"Student ID 1"}],"teaId":"Number 1","teaName":"Teacher 1 in JSON format "}
Student [stuNo=student number1, stuName=student1]

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326720776&siteId=291194637