账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    前端拼接json字符串
    26
    0

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

    0
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 11 元积分
        全部回答
    • 0
    • :+酷::b 普通会员 1楼

      在前端中,我们可以使用JavaScript的JSON.parse()函数将JSON字符串解析为JavaScript对象,然后使用JSON.stringify()函数将JavaScript对象转换回JSON字符串。以下是一个简单的例子:

      ```javascript var jsonStr = '{"name": "John", "age": 30, "city": "New York"}';

      // 使用JSON.parse()函数将JSON字符串解析为JavaScript对象 var obj = JSON.parse(jsonStr);

      // 使用JSON.stringify()函数将JavaScript对象转换回JSON字符串 var str = JSON.stringify(obj);

      console.log(str); // 输出:{"name":"John","age":30,"city":"New York"} ```

      在这个例子中,我们首先定义了一个包含JSON数据的字符串。然后,我们使用JSON.parse()函数将这个字符串解析为一个JavaScript对象。最后,我们使用JSON.stringify()函数将这个JavaScript对象转换回一个JSON字符串。

      注意,JSON.parse()和JSON.stringify()函数都接受一个可选的第二个参数,这个参数是一个转换函数。如果这个参数是函数,那么JSON.parse()和JSON.stringify()函数都会把这个函数作为转换函数。例如:

      ```javascript var jsonStr = '{"name": "John", "age": 30, "city": "New York"}'; var obj = JSON.parse(jsonStr); var str = JSON.stringify(obj, function(key, value) { return key + " = " + value; });

      console.log(str); // 输出:{"name":"John","age":30,"city":"New York"} ```

      在这个例子中,我们定义了一个包含JSON数据的字符串,并使用JSON.parse()函数将这个字符串解析为一个JavaScript对象。然后,我们使用JSON.stringify()函数将这个JavaScript对象转换回一个JSON字符串,这个字符串包含了对JSON对象的转换函数。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部