使用node.js从json中删除单引号

时间:2014-03-08 09:55:21

标签: jquery node.js

我有一个像这样的字符串

var record ={ '"test":"Connect_Disconnect","jobid": "65","os":"Windows NT","report":"Verify Wireless Interface present and state is Disconnected:OK<br>Verify Profile not present on the Client:OK Client:OK<br>Verify Profile Not Present on the Client:OK<br>"': '' }

从此我必须删除单引号

使用node.js怎么可能? 我厌倦了它无法正常工作的事实

record = record.replace(/"/g, ""); 

我想要输出如下

{
  "test": "Connect_Disconnect",
  "jobid": "65",
  "os": "Windows NT",
  "report": "Verify Wireless Interface present and state is Disconnected:OK<br>Verify Profile not present on the Client:OK<br>Add Profile to the Client:OK<br>Verify Profile Added Present on the Client:OK<br>Connecting to Access Point:OK<br>Verify the State is Connected:OK<br>Disconnecting from Access Point:OK<br>Verify the State is Disconnected:OK<br>Delete Profile to the Client:OK<br>Verify Profile Not Present on the Client:OK<br>"
};

2 个答案:

答案 0 :(得分:3)

试试这个:

var record ={ '"test":"Connect_Disconnect","jobid": "65","os":"Windows NT","report":"Verify Wireless Interface present and state is Disconnected:OK<br>Verify Profile not present on the Client:OK Client:OK<br>Verify Profile Not Present on the Client:OK<br>"': '' }
for(var key in record) { // get your string from original json object
    break;
}
record = JSON.parse('{' + key + '}'); // parse string to new json object

答案 1 :(得分:1)

全局JSON对象具有parse方法,可生成所需的输出。 node.js docs中没有记录这一点的原因是因为它是语言的一部分。

var record = JSON.parse('{"test":"Connect_Disconnect","jobid": "65","os":"Windows NT","report":"Verify Wireless Interface present and state is Disconnected:OK<br>Verify Profile not present on the Client:OK Client:OK<br>Verify Profile Not Present on the Client:OK<br>"}');

(注意,添加了{}来包装整个字符串)。