我有一个对象,我想从中提醒值“tr_name”
包含此值的对象位于:
[{"cells":[{"type":"basic.Rect","position":{"x":290,"y":450},"size":{"width":90,"height":54},"angle":0,"id":"0c53af29-a635-4e74-8759-6d31472e0ffb","embeds":"","z":1,"wi_name":"","wi_displayName":"","wi_description":"","wi_join":"<None>","wi_split":"<None>","wi_performingUser":"","wi_workitem_variables":"","wi_expected_activity_time":null,"wi_expected_user_time":null,"wi_maximum_activity_time":null,"wi_initial_delay":null,"wi_time_unit":"Seconds","wi_required_transitions_for_AND_JOIN":null,"wi_custom_page":"","attrs":{"rect":{"fill":"#0000ff","width":50,"height":30,"rx":2,"ry":2,"stroke-width":1,"stroke-dasharray":"0"},"text":{"fill":"#ffffff","text":"Activity","font-size":10,"font-family":"Arial","stroke":"#000000","stroke-width":0,"font-weight":400}}},{"type":"link","source":{"id":"0c53af29-a635-4e74-8759-6d31472e0ffb"},"target":{"x":720,"y":480},"id":"7086cf67-2254-4113-a9ca-564d4dd8c0f9","embeds":"","z":2,"smooth":false,"manhattan":false,"labels":[],"attrs":{".marker-source":{"d":"M 10 0 L 0 5 L 10 10 z","transform":"scale(1)","fill":"#000000"},".marker-target":{"d":"M 10 0 L 0 5 L 10 10 z","transform":"scale(1)","fill":"#000000"},".connection":{"stroke":"#000000","stroke-width":1,"stroke-dasharray":"0"},".attributes":{"tr_name":"AAAAAAAAAAAAAAA","tr_description":"","tr_rules":"Rule 1","tr_value":"true","tr_rule_source":"BPM"}}}]},"{}","{}","{}","{}","{}"]
到目前为止,我尝试了这样的警报而没有结果:
var selectedObjectDataText = JSON.stringify(this.selection.invoke('toJSON'));
var selectedObjectDataJSON = JSON.parse(selectedObjectDataText);
alert(selectedObjectDataJSON[0].[".attributes"]["tr_name"]);
我该怎么办呢?
答案 0 :(得分:1)
您有一个JavaScript对象,您可以按如下方式获取tr_name的值(我已经使用JSON格式化程序格式化了您提供的JSON)。
您不需要使用JSON.stringify
和JSON.parse
,他们会执行以下操作
JSON.stringify
将JavaScript对象转换为JSON文本,并将该JSON文本存储在字符串中。
JSON.parse
将一串JSON文本转换为JavaScript对象。
var json = [
{
"cells":[
{
"type":"basic.Rect",
"position":{
"x":290,
"y":450
},
"size":{
"width":90,
"height":54
},
"angle":0,
"id":"0c53af29-a635-4e74-8759-6d31472e0ffb",
"embeds":"",
"z":1,
"wi_name":"",
"wi_displayName":"",
"wi_description":"",
"wi_join":"<None>",
"wi_split":"<None>",
"wi_performingUser":"",
"wi_workitem_variables":"",
"wi_expected_activity_time":null,
"wi_expected_user_time":null,
"wi_maximum_activity_time":null,
"wi_initial_delay":null,
"wi_time_unit":"Seconds",
"wi_required_transitions_for_AND_JOIN":null,
"wi_custom_page":"",
"attrs":{
"rect":{
"fill":"#0000ff",
"width":50,
"height":30,
"rx":2,
"ry":2,
"stroke-width":1,
"stroke-dasharray":"0"
},
"text":{
"fill":"#ffffff",
"text":"Activity",
"font-size":10,
"font-family":"Arial",
"stroke":"#000000",
"stroke-width":0,
"font-weight":400
}
}
},
{
"type":"link",
"source":{
"id":"0c53af29-a635-4e74-8759-6d31472e0ffb"
},
"target":{
"x":720,
"y":480
},
"id":"7086cf67-2254-4113-a9ca-564d4dd8c0f9",
"embeds":"",
"z":2,
"smooth":false,
"manhattan":false,
"labels":[
],
"attrs":{
".marker-source":{
"d":"M 10 0 L 0 5 L 10 10 z",
"transform":"scale(1)",
"fill":"#000000"
},
".marker-target":{
"d":"M 10 0 L 0 5 L 10 10 z",
"transform":"scale(1)",
"fill":"#000000"
},
".connection":{
"stroke":"#000000",
"stroke-width":1,
"stroke-dasharray":"0"
},
".attributes":{
"tr_name":"AAAAAAAAAAAAAAA",
"tr_description":"",
"tr_rules":"Rule 1",
"tr_value":"true",
"tr_rule_source":"BPM"
}
}
}
]
},
"{}",
"{}",
"{}",
"{}",
"{}"
];
var result = json[0].cells[1]['attrs']['.attributes']['tr_name'];
console.log(result);
&#13;
答案 1 :(得分:1)
您的数据路径不完整。
<强>说明强>
JSON方法
JSON.parse
将字符串(JSON格式化)转换为 JSON对象 JSON.stringify
将 JSON对象转换为字符串ALERT JSON
JSON.stringify
)
alert(selectedObjectDataJSON[0].cells[1].attrs['.attributes'].tr_name);
JSON.stringify
之前alert
alert(JSON.stringify(selectedObjectDataJSON[0].cells[1].attrs['.attributes']);
LIVE DEMO