您的位置:首页 > Web前端 > JavaScript

json错误解析net.sf.ezmorph.bean.MorphDynaBean cannot be cast to

2017-10-24 11:07 567 查看
先说下json字符串转对象的方法:

//JSONObject转换为bean的两种方法:

JSONObject json = JSONObject.fromObject(temp);

User user1 = (User) JSONObject.toBean(json, User.class);

JsonConfig jsonConfig =
new JsonConfig();

jsonConfig.setRootClass(User.class);

User user2 = (User) JSONSerializer.toJava(json, jsonConfig);

//JSONArray转换为List<bean>的两种方法:

JSONArray userArray = JSONArray.fromObject("[" + json.toString() + "]");

List<User> userList1 = (List<User>) JSONArray.toCollection(userArray, User.class);

List<User> userList2 = (List<User>) JSONSerializer.toJava(userArray, jsonConfig);

json错误解析net.sf.ezmorph.bean.MorphDynaBean

今天在做json字符串转对象时,遇到字符串里有数组,转换不报错,能看到值,但是就是不能取到的问题。

我的json字符串:

{modelId:1,firstChl:{chlPatentId:376,chlCount:4,maxInd:9,rowNum:3,startInd:3,endInd:13,tzggChlId:709,tzggChlInd:3,videoChlId:704,videoChlInd:1,marqChlId:1,marqChlInd:1,weaChlId:1,weaChlInd:1},secondChl:[{chlNum:6},{chlNum:8},{chlNum:10},{chlNum:10}]}

显然secondChl里是一个数组,我定义了一个对象与它对应。

最外层对象:

public class TplTmpInitVo {

private String modelId;

private TplFirstChlVo firstChl;

private List<TplSecondChlVo> secondChl;

public String getModelId() {

return modelId;

}

public void setModelId(String modelId) {

this.modelId = modelId;

}

public TplFirstChlVo getFirstChl() {

return firstChl;

}

public void setFirstChl(TplFirstChlVo firstChl) {

this.firstChl = firstChl;

}

public List<TplSecondChlVo> getSecondChl() {

return secondChl;

}

public void setSecondChl(List<TplSecondChlVo> secondChl) {

this.secondChl = secondChl;

}

}

firstChl对象

public class TplFirstChlVo {

private String chlPatentId;

private int chlCount;

private int maxInd;

private int rowNum;

private int startInd;

private int endInd;

private String tzggChlId;

private int tzggChlInd;

private String videoChlId;

private int videoChlInd;

private String marqChlId;

private int marqChlInd;

private String weaChlId;

private int weaChlInd;

public String getChlPatentId() {

return chlPatentId;

}

public void setChlPatentId(String chlPatentId) {

this.chlPatentId = chlPatentId;

}

public int getChlCount() {

return chlCount;

}

public void setChlCount(int chlCount) {

this.chlCount = chlCount;

}

public int getMaxInd() {

return maxInd;

}

public void setMaxInd(int maxInd) {

this.maxInd = maxInd;

}

public int getRowNum() {

return rowNum;

}

public void setRowNum(int rowNum) {

this.rowNum = rowNum;

}

public int getStartInd() {

return startInd;

}

public void setStartInd(int startInd) {

this.startInd = startInd;

}

public int getEndInd() {

return endInd;

}

public void setEndInd(int endInd) {

this.endInd = endInd;

}

public String getTzggChlId() {

return tzggChlId;

}

public void setTzggChlId(String tzggChlId) {

this.tzggChlId = tzggChlId;

}

public int getTzggChlInd() {

return tzggChlInd;

}

public void setTzggChlInd(int tzggChlInd) {

this.tzggChlInd = tzggChlInd;

}

public String getVideoChlId() {

return videoChlId;

}

public void setVideoChlId(String videoChlId) {

this.videoChlId = videoChlId;

}

public int getVideoChlInd() {

return videoChlInd;

}

public void setVideoChlInd(int videoChlInd) {

this.videoChlInd = videoChlInd;

}

public String getMarqChlId() {

return marqChlId;

}

public void setMarqChlId(String marqChlId) {

this.marqChlId = marqChlId;

}

public int getMarqChlInd() {

return marqChlInd;

}

public void setMarqChlInd(int marqChlInd) {

this.marqChlInd = marqChlInd;

}

public String getWeaChlId() {

return weaChlId;

}

public void setWeaChlId(String weaChlId) {

this.weaChlId = weaChlId;

}

public int getWeaChlInd() {

return weaChlInd;

}

public void setWeaChlInd(int weaChlInd) {

this.weaChlInd = weaChlInd;

}

}

secondChl对象

public class TplSecondChlVo {

private int chlNum;

public int getChlNum() {

return chlNum;

}

public void setChlNum(int chlNum) {

this.chlNum = chlNum;

}

}

表面看起来这样的转换没有问题是吧,实际在转换过程中也确实没有问题。

转换方法:

JSONObject jsonObj = JSONObject.fromObject(initStr);

TplTmpInitVo tplTmp = null;

tplTmp = (TplTmpInitVo)JSONObject.toBean(jsonObj,TplTmpInitVo.class);

使用转换后的结果,去secondChl时:



值都能看到,但是一旦使用TplSecondChlVo 的getChlNum方法就报错。

原因大家可能知道,是因为字符串里含有数组,要转换的对象里含有list对象,也就是复杂对象。

解决办法:

将调用改为:

JSONObject jsonObj = JSONObject.fromObject(initStr);

TplTmpInitVo tplTmp = null;

Map<String, Class> classMap = new HashMap<String, Class>();

classMap.put("secondChl", TplSecondChlVo.class);

tplTmp = (TplTmpInitVo)JSONObject.toBean(jsonObj,TplTmpInitVo.class,classMap);

完美解决。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐