1.后端想要的json字符串如下
{
"key":"q",
"name":"请假流程",
"productID":"1",
"nodeList":[
{
"id":"241",
"node_name":"新步骤",
"creation_time":"",
"creation_time_stamp":"",
"role_id":"person",
"next_role_id":"manager",
"last_step_id":"",
"next_step_id":"242",
"admin_id":"",
"remark":"",
"workflow_id":"h",
"style":"width:121px;height:41px;line-height:41px;color:#0e76a8;left:532px;top:186px;",
"product_id":"1",
"sort":""
},
{
"id":"242",
"node_name":"新步骤",
"creation_time":"",
"creation_time_stamp":"",
"role_id":"person",
"next_role_id":"manager",
"last_step_id":"241",
"next_step_id":"243",
"admin_id":"",
"remark":"",
"workflow_id":"h",
"style":"width: 121px; height: 41px; line-height: 41px; color: rgb(14, 118, 168); left: 1030px; top: 374px;",
"product_id":"1",
"sort":""
},
{
"id":"243",
"node_name":"新步骤",
"creation_time":"",
"creation_time_stamp":"",
"role_id":"person",
"next_role_id":"manager",
"last_step_id":"242",
"next_step_id":"",
"admin_id":"",
"remark":"",
"workflow_id":"h",
"style":"width: 121px; height: 41px; line-height: 41px; color: rgb(14, 118, 168); left: 608px; top: 392px;",
"product_id":"1",
"sort":""
}
]
}
2.我开始拼接字符串
var aProcessData= {
key: "q",
name:"请假流程",
productID:"1",
nodeList:[
]
};
var nodeListObj={
id:id,
node_name:node_name,
creation_time:"",
creation_time_stamp:"",
role_id:"person",
next_role_id:"manager",
last_step_id:last_step_id,
next_step_id:next_step_id,
admin_id:"",
remark:"",
workflow_id:"h",
style:style,
product_id:"1",
sort:""
};
遍历数组,把nodeListObj添加进aProcessData.nodeList
aProcessData.nodeList.push(nodeListObj)
3.感觉这样写死是不对的啊,如果要加一些判断之类的,比如当next_step_id为空时,不要这个字段
那我这样子写可以吗
var nodeListObj={};
nodeListObj.id=id;
nodeListObj.node_name=node_name;
nodeListObj.creation_time="";
nodeListObj.creation_time_stamp="";
nodeListObj.role_id="person";
if(last_step_id !="") nodeListObj.last_step_id=last_step_id;
if(next_step_id !="") nodeListObj.next_step_id=next_step_id;
4.有没有更优雅的方式去拼接json字符串
1、删除字段可以使用delete
2、一个简单的方式
3、再根据条件删除,修改属性,就好了。