用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' }热门推荐
10 幼儿祝福语大全简短卡片
11 孩子过年祝福语简短独特
12 女生 节日祝福语简短大气
13 接春的祝福语简短
14 老师对学校祝福语简短
15 新年简短的祝福语爱情
16 佛教生日祝福语简短大全
17 群里祝福语简短幽默
18 给长辈祝福语简短大全