zl程序教程

您现在的位置是:首页 >  后端

当前栏目

json 字符串包含数组转换为object对象是报异常java.lang.ClassCastException: net.sf.ezmorph.bean.MorphDynaBean cannot be cast to

2023-09-14 08:57:44 时间

前台传到后台的json字符串

   前台实现这种格式json字符串方式:

  

function contentFun(){
      	respType = respTypeFun();
      	return "{\"respType\":\"" + respType + "\",\"content\":\"" + content + "\",\"name\":\"" + name + "\"}";
}

function respTypeFun(){
      if(respType == undefined || respType == ""){
      	respType = "res_ejcd";	
      	return respType;	
      }
    return respType;
}
var tableId = document.getElementById("tableId").getElementsByTagName("div");
      	 for(var i = 0 ; i < choiceNum ; i ++){
      	 	if(tableId[i].hasAttribute("reldate")){
      	 		 var d = eval('('+ tableId[i].getAttribute("reldate") +')');
      	 		 d.subMenu = [];
      	 		
      	 		var t = i + 1;
      	 		var cNode = document.getElementById("zicaidan" + t).getElementsByTagName("div");
      	 		for(var j = 0 ; j < cNode.length ; j ++){
      	 			if(cNode[j].hasAttribute("reldate")){
	      	 				
	      	 			d.subMenu[j] = eval('('+ cNode[j].getAttribute("reldate") +')');
      	 			}
      	 		}
      
      	 		console.info(d);
      	 		d = JSON.stringify(d);
      	 		console.info(d);
      	 		menuStr += d + ","; 
      	 	}
      	 }
      	 menuStr = "[" +menuStr.substring(0, menuStr.length - 1) + "]";

  

  contentFun()将获取到的数据保存到一个节点中 该节点属性为reldate如下:

 <table class="caiduannum" style="border: none;width: 255px;" cellpadding="0" cellspacing="0" id="tableId">
       <tbody>
           <tr>
             <td>
                <div id="maincd1" onclick="menuChoice(this)" class="maincd" style="height: 45px;width: 85px;line-height: 45px;text-align: center; 
reldate='{"respType":"res_ejcd","content":"aa;dsf;t ;","name":"菜单一"}'>
<span class="menuSpan"
>菜单一</span></div>
             </td>
             <td>
                <div id="maincd2" onclick="menuChoice(this)" class="maincd" style="height: 45px;width: 85px;line-height: 45px;text-align: center;" 
reldate='{"respType":"res_ejcd","content":"fg ;","name":"菜单二"}' >
<span class="menuSpan" >菜单二</span>
</div>
</td>
<td>
</tr>
</tbody>
</table>

  

 

 

 

通过上面方法即可获取到下面json字符串,将此字符串传给后台

[
    {
        "respType":"res_ejcd",
        "content":"aa;dsf;t ;",
        "name":"菜单一",
        "subMenu":[
            {
                "respType":"res_ejcd",
                "content":"aa",
                "name":"菜单一"
            },
            {
                "respType":"res_gjz",
                "content":"的风格",
                "name":"dsf"
            },
            {
                "respType":"res_ejcd",
                "content":"t ",
                "name":"菜单一"
            }
        ]
    },
    {
        "respType":"res_ejcd",
        "content":"fg ;",
        "name":"菜单二",
        "subMenu":[
            null,
            {
                "respType":"res_url",
                "content":"http://www.baidu.com",
                "name":"fg "
            }
        ]
    }
]

  后台对其处理方式:

package com.renmai.weixin.weixin.model;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.renmai.db.model.Users;

public class WxMenu {
    private String id;

    private String name;

    private String _key;

    private String url;

    private String mediaId;

    private String content;

    private String pId;

    private String respType;
    
    private List<WxMenu> subMenu;
    


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id == null ? null : id.trim();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }

    public String get_key() {
		return _key;
	}

	public void set_key(String _key) {
		this._key = _key;
	}

	public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url == null ? null : url.trim();
    }

    public String getMediaId() {
        return mediaId;
    }

    public void setMediaId(String mediaId) {
        this.mediaId = mediaId == null ? null : mediaId.trim();
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content == null ? null : content.trim();
    }

    public String getpId() {
        return pId;
    }

    public void setpId(String pId) {
        this.pId = pId == null ? null : pId.trim();
    }

    public String getRespType() {
        return respType;
    }

    public void setRespType(String respType) {
        this.respType = respType == null ? null : respType.trim();
    }

	public List<WxMenu> getSubMenu() {
		return subMenu;
	}

	public void setSubMenu(List<WxMenu> subMenu) {
		this.subMenu = subMenu;
	}

}

  

public void add(){
		System.out.println(menuStr);
		JSONArray menuJson = JSONArray.fromObject(menuStr);
		JSONObject jsonObject ;
		List<WxMenu> wxMenus = new ArrayList<WxMenu>();
		List<WxMenu> subMenu = new ArrayList<WxMenu>();
		Map<String, Class> classMap = new HashMap<String, Class>();  
		classMap.put("subMenu", WxMenu.class);
		WxMenu menu = new WxMenu();
		for (int i = 0; i < menuJson.size(); i++) {
			jsonObject = menuJson.getJSONObject(i);
			menu =  (WxMenu) JSONObject.toBean(jsonObject, WxMenu.class , classMap);//处理异常关键MorphDynaBean cannot ,处理链接
			menu.setId(UUID.randomUUID().toString());
			
			subMenu = menu.getSubMenu();
			wxMenuService.insert(menu);
			for (WxMenu wxMenu : subMenu) {
				if(wxMenu != null){
					wxMenu.setId(UUID.randomUUID().toString());
					wxMenu.setpId(menu.getId());
					
					wxMenuService.insert(wxMenu);
				}
			}
			wxMenus.add(menu);
		}
		try {
			getHttpResponse().getWriter().print("ok");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}