前端拼接json字符串

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字符串

阅读 4.7k
2 个回答

1、删除字段可以使用delete

var obj={a:1,b:2};
delete obj.a;
console.log(obj);
// {b: 2}

2、一个简单的方式

var yourObj=JSON.parse(`{
    "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":""
        }
    ]
}`)

3、再根据条件删除,修改属性,就好了。

没有了,对象不就是这样的吗?不要字段是不可能,肯定是为空''或者null就看你和后台商议了

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题