使用 JavaScript 去除引号以转换为 JSON 对象?
javascriptweb developmentobject oriented programming更新于 2025/11/12 18:20:17
为此,您可以将 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' }
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

