简体   繁体   English

如何从json对象中删除双引号

[英]How to remove double quotes from json object

My JSON object looks like below我的 JSON 对象如下所示

"{'EN':{'APP_TITLE':'TESTHEADING'}}"

Can some help me on removing the double quotes at first position and last position I tried with regex but not able to remove it.可以帮助我删除第一个位置和最后一个位置的双引号,我尝试使用正则表达式但无法删除它。

Use .substring() like below:使用.substring()如下:

 let input = "\\"{'EN':{'APP_TITLE':'TESTHEADING'}}\\""; let output = input.substring(1, input.length - 1); console.log(`Input: ${input} \\nOutput: ${output}`);

  • To remove the double quotes at the beginning and end, you can use substring .要删除开头和结尾的双引号,可以使用substring It take a start index (here 1 skipping the first character) and an end (here str.length - 1 skipping the last)它需要一个开始索引(这里是1跳过第一个字符)和一个结束(这里是str.length - 1跳过最后一个字符)
  • To have a valid JSON string you also need to replace the single quotes with double quotes .要获得有效的JSON 字符串,您还需要将单引号替换为双引号 To do that you can use a regex with the function replace .为此,您可以使用带有函数replace的正则表达式。 The g option replace all occurences of your target character ' => " g选项替换所有出现的目标字符' => "
  • To load this as an object (for further processing) in JavaScript, use JSON.parse .要在 JavaScript 中将其作为对象加载(用于进一步处理),请使用JSON.parse This will convert your JSON string into an actual object.这会将您的 JSON 字符串转换为实际对象。

Combining the 3 steps will give you something like this结合这 3 个步骤会给你这样的东西

 const str = "\\"{'EN':{'APP_TITLE':'TESTHEADING'}}\\""; const withoutQuotes = str .substring(1, str.length - 1) .replace(/'/g, '"'); const actualJSON = JSON.parse(withoutQuotes); console.log(actualJSON);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM