json 字符串包含数组转换为object对象是报异常java.lang.ClassCastException: net.sf.ezmorph.bean.MorphDynaBean cannot be cast to
前台传到后台的json字符串
前台实现这种格式json字符串方式:
1
2
3
4
5
6
7
8
9
10
11
12
|
function contentFun(){
respType = respTypeFun();
return "{\"respType\":\"" + respType + "\",\"content\":\"" + content + "\",\"name\":\"" + name + "\"}";
}
function respTypeFun(){
if(respType == undefined || respType == ""){
respType = "res_ejcd";
return respType;
}
return respType;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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如下:
1
2
3
4
5
|
<
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
>
|
1
2
3
|
</
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字符串,将此字符串传给后台
1
|
[ { "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 " } ] }]
|
后台对其处理方式:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
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;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
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();
}
}
|