- 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 条
- 全部回答
-
:+酷::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对象的转换函数。
- 扫一扫访问手机版
回答动态

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器更新之后。服务器里面有部分玩家要重新创建角色是怎么回事啊?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题函数计算不同地域的是不能用内网吧?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题ARMS可以创建多个应用嘛?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题在ARMS如何申请加入公测呀?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题前端小程序接入这个arms具体是如何接入监控的,这个init方法在哪里进行添加?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器刚到期,是不是就不能再导出存档了呢?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器的游戏版本不兼容 尝试更新怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器服务器升级以后 就链接不上了,怎么办?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器转移以后服务器进不去了,怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器修改参数后游戏进入不了,是什么情况?预计能赚取 0积分收益
- 回到顶部
- 回到顶部
