net.sf.ezmorph.bean.MorphDynaBean cannot be cast to com.console.demo.web.model.XXX

前景:web

net.sf.ezmorph.bean.MorphDynaBean cannot be cast to com.console.demo.web.model.XXXjson

//jsonObject:全部參數
//FreightTemplate對象裏面包含一個 private List<FreightTemplateCity> freightTemplateCity; 相似於一父多子
FreightTemplate freightTemplate = (FreightTemplate) JSONObject.toBean(JSONObject.fromObject(jsonObject.get("freightTemplate")), FreightTemplate.class);
//先新增freightTemplate
businessDao.insertSelective(freightTemplate);
//取出子list對象
List<FreightTemplateCity> freightTemplateCities = freightTemplate.getFreightTemplateCity();
//遍歷新增子對象
//PS:這句for循環報的錯
for (FreightTemplateCity freightTemplateCity : freightTemplateCities) {
    freightTemplateCity.setTemplateId(freightTemplate.getId());
    businessDao.insertFreightTemplateCity(freightTemplateCity);
}

發現個人子對象自動被轉換成了net.sf.ezmorph.bean.MorphDynaBean對象,找了半天不知道哪裏轉成這個的,因而想到直接把對象

JSONObject object = (JSONObject) jsonObject.get("freightTemplate");//從全部對象裏取到父對象,轉成JSONObject類型
JSONArray array = (JSONArray) object.get("freightTemplateCity");//從JSONObject裏面取到"freightTemplateCity"轉成JSONArray格式

而後JSONArray就能夠直接轉list了,下面貼代碼get

FreightTemplate freightTemplate = (FreightTemplate) JSONObject.toBean(JSONObject.fromObject(jsonObject.get("freightTemplate")), FreightTemplate.class);
//先新增freightTemplate
businessDao.insertSelective(freightTemplate);
//------------這一段轉換對象類型----------------
JSONObject object = (JSONObject) jsonObject.get("freightTemplate");
JSONArray array = (JSONArray) object.get("freightTemplateCity");
List<FreightTemplateCity> list = JSONArray.toList(array, new FreightTemplateCity(), new JsonConfig());
//----------------------------
for (FreightTemplateCity freightTemplateCity : list) {
     freightTemplateCity.setTemplateId(freightTemplate.getId());
     businessDao.insertFreightTemplateCity(freightTemplateCity);
}

可是由於不知道具體發生轉換的緣由,不知道這樣作有沒有什麼影響,若是有其餘好的辦法,望不吝嗇指教一下it

相關文章
相關標籤/搜索