如何模拟php';s json_使用json.stringify在javascript/node js中编码?

如何模拟php';s json_使用json.stringify在javascript/node js中编码?,php,json,node.js,stringify,Php,Json,Node.js,Stringify,我正在寻找一种从节点js模仿php的json_encode行为的方法。下面的示例显示了php如何处理对象中的url,该对象使用json_编码: <? $foo['url'] = "http://example.com/note/that/the/slashes/get/backslashed"; echo json_encode($foo); ?> 在此处使用node.js和JSON.stringify函数: var foo = new Object(); foo.url = "h

我正在寻找一种从节点js模仿php的json_encode行为的方法。下面的示例显示了php如何处理对象中的url,该对象使用json_编码:

<?
$foo['url'] = "http://example.com/note/that/the/slashes/get/backslashed";
echo json_encode($foo);
?>
在此处使用node.js和JSON.stringify函数:

var foo = new Object();
foo.url = "http://example.com/note/that/the/slashes/do/not/get/backslashed"
console.log(JSON.stringify(foo));
我观察的是这个输出:

 {"url":"http://example.com/note/that/the/slashes/do/not/get/backslashed"}
您知道一种干净的方法可以让JSON.stringify以与PHP相同的方式运行吗

额外信息:我意识到正确的json编码可能不需要这些斜杠,但我正在将json编码的对象发送到远程服务器,我无法控制,也不喜欢没有反斜杠的对象


更多额外信息:我试着加入我自己的反斜杠,然后调用JSON.stringify,但是JSON.stringify确实正确地避开了反斜杠,所以我最终得到了\\/而不是\/这正是我想要的。

如果只是斜杠,那么在转换后,你可以用
\/
替换
/

如果只有斜杠,则可以在转换后将
/
替换为
\/

哈!!我怎么没看到呢?谢谢哈我怎么没看到呢?谢谢
 {"url":"http://example.com/note/that/the/slashes/do/not/get/backslashed"}