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

前台传到后台的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" οnclick="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" οnclick="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();
		}
	}
	

  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值