Javascript在使用stringify时不断添加反斜杠
我刚刚升级到Ruby 5.1.4,我以前使用的一些javascript代码停止工作,我似乎不知道为什么 每次我像这样设置对象的属性时Javascript在使用stringify时不断添加反斜杠,javascript,ruby-on-rails,ruby,Javascript,Ruby On Rails,Ruby,我刚刚升级到Ruby 5.1.4,我以前使用的一些javascript代码停止工作,我似乎不知道为什么 每次我像这样设置对象的属性时 FLOW.nodes.forEach( function( node ) { _nodes.push({ "step_type": node.type, "internal_id": node.getId({ useMap: useMap }), "step_a
FLOW.nodes.forEach( function( node ) {
_nodes.push({
"step_type": node.type,
"internal_id": node.getId({ useMap: useMap }),
"step_attributes" : {
"shape": node.shape,
"icon": node.icon,
"title": node.title,
"subtitle": node.subTitle,
"connectors": JSON.stringify( node.connectors ),
"tags": JSON.stringify( node.tags ),
"allowed_connections": JSON.stringify( node.allowedConnections ),
"position": JSON.stringify( node.position ),
"configured": node.configured,
"socket": JSON.stringify( node.socket ),
"buttons": JSON.stringify( node.buttons ),
"color": node.color
},
"properties": JSON.stringify( FLOW.getNodeProperties( node.getId() ) )
});
});
每次我保存它都会不断地向属性添加更多的反斜杠,我不知道为什么
这是它打印出来的样子
"properties": "\"{\\\"tags\\\": \\\"erewerwre\\\",\\\"tagged_present\\\": true,\\\"tagged_future\\\": false,\\\"tagged_present_future\\\": false,\\\"remove_from_other_flows\\\": false}\""
如果它再次在那里运行,它将继续添加更多的反斜杠
知道如何解决这个问题吗?查看代码和结果,
FLOW.getNodeProperties
返回一个包含JSON的字符串。通过对其应用JSON.stringify
,您将对其进行第二次编码,例如:
函数getNodeProperties(){
返回JSON.stringify({some:“property”});
}
var结果={
属性:JSON.stringify(getNodeProperties())
};
console.log(result.properties)代码>为什么在JavaScript内部调用
stringify
?显然,这将创建重复的引号。您正在直接使用JavaScript对象。键上的引号是无关的,这不是JSON数据。这是JavaScript数据。