用JavaScript删除引号以转换为JSON对象?
为此,您可以replace()
与一起使用parse()
。以下是代码-
示例
var studentDetails = `"{""name"": ""John"",""subjectName"": ""Introduction To JavaScript""}"`; console.log("The actual object="); console.log(studentDetails); var removeFirstAndLast = studentDetails.substring(1,studentDetails.length-1) var removeDoubleQuotes = removeFirstAndLast.replace(/""/gi, `"`) console.log(removeDoubleQuotes) var output = JSON.parse(removeDoubleQuotes); console.log(output);
要运行以上程序,您需要使用以下命令-
node fileName.js.
在这里,我的文件名为demo103.js。
输出结果
这将产生以下输出-
PS C:\Users\Amit\JavaScript-code> node demo103.js The actual object= "{""name"": ""John"",""subjectName"": ""Introduction To JavaScript""}" {"name": "John","subjectName": "Introduction To JavaScript"} { name: 'John', subjectName: 'Introduction To JavaScript' }