在json字符串转java bean时,一般的对象,可以直接转,如:一个学生类,属性有姓名、年龄等
public class Student implements java.io.Serializable{ /** * @Fields serialVersionUID : TODO */ private static final long serialVersionUID = -1943961352036134112L; private String sname; private Integer age; public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
由字符串转java bean时可以使用以下方法:
JSONObject jobj = JSONObject.fromObject(str); Student s = (Student) JSONObject.tobean(jobj,Student.class);
是没有问题的,但是如果属性中含有复杂的类型,当其中属性有类似List , Map ,ArrayList、自定义的类型,如List<Teacher> teachers, 就不可以了。 会报错:MorphDynaBean cannot be cast to con.test……
在JSONObject.toBean的时候如果转换的类中有集合,可以先定义Map<String, Class> classMap = new HashMap<String, Class>();在classMap中put你要转换的类中的集合名,像:classMap.put("teachers", Teacher.class);然后在toBean()的时候把参数加上, 像:Student student=(Student) JSONObject.toBean(str, Student.class, classMap);
比如我定义了如下类型:
import java.util.List; /** * * <P>Tiltle: AnalysisModel.java </P> * <P>Description:模型类 </P> * @author biyutong * @date Jun 11, 2012 9:37:54 AM * @version:V1.0 */ public class AnalysisModel implements java.io.Serializable{ /** * 固定条件 */ private List<Condition> regularCondtionList;// /** * 动态条件 */ private List<ConditionField> dynamicConFieldList; /** * 结果 */ private List<ResultField> resultField; /** * 获取前多少条数据 */ private Integer topnum; /** * 顺序还是随机 */ private String sortFlag; /** * 备注 */ private String remark; /** * 模型名称 */ private String modelName; /** * 对应的xml文件名称 */ private String fileName; public List<Condition> getRegularCondtionList() { return regularCondtionList; } public void setRegularCondtionList(List<Condition> regularCondtionList) { this.regularCondtionList = regularCondtionList; } public List<ConditionField> getDynamicConFieldList() { return dynamicConFieldList; } public void setDynamicConFieldList(List<ConditionField> dynamicConFieldList) { this.dynamicConFieldList = dynamicConFieldList; } public Integer getTopnum() { return topnum; } public void setTopnum(Integer topnum) { this.topnum = topnum; } public String getSortFlag() { return sortFlag; } public void setSortFlag(String sortFlag) { this.sortFlag = sortFlag; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } public String getModelName() { return modelName; } public void setModelName(String modelName) { this.modelName = modelName; } public List<ResultField> getResultField() { return resultField; } public void setResultField(List<ResultField> resultField) { this.resultField = resultField; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } }
在转化的时候需要这样来写:
Map<String, Class> classMap = new HashMap<String, Class>(); classMap.put("regularCondtionList", Condition.class); classMap.put("dynamicConFieldList", ConditionField.class); classMap.put("resultField", ResultField.class); AnalysisModel analysisModel=(AnalysisModel) JSONObject.toBean(str, AnalysisModel.class, classMap);
from: http://biyutong.iteye.com/blog/1566708