[英]Remove empty double quotes from string javascript
Hello so I'm trying to clean a json string that i have for example:您好,我正在尝试清理我拥有的 json 字符串,例如:
" .tagline-wrapper ":{
" padding-top":"398px",
" font-size":" 1.8rem",
" "
},
".tagline ":{
" padding-top":"398px",
" font-size":" 1.8rem",
" ",
},
".wrapper":{
" padding-top":"398px",
" font-size":" 1.8rem",
""
},
I need to remove the double quotes that are empty or with white space it could have multiple white space, i tried to remove consecutive white space and then replace the quotes as so:我需要删除空的双引号或带有空格的双引号,它可能有多个空格,我尝试删除连续的空格,然后将引号替换为:
str = str .replace("\" \"",'');
str = str .replace("\"\"",'');
but nothing works, i should also remove the comma but i guess i can remove consecutive commas ignoring white space but if you can help with that too that would be great.但没有任何效果,我也应该删除逗号,但我想我可以删除忽略空格的连续逗号,但如果你也能提供帮助,那就太好了。 The output that i need is:
我需要的 output 是:
" .tagline-wrapper":{
" padding-top":"398px",
" font-size":" 1.8rem",
},
".tagline":{
" padding-top":"398px",
"font-size":" 1.8rem",
},
".wrapper":{
" padding-top":"398px",
" font-size":" 1.8rem",
},
so that i can parse the json这样我就可以解析 json
Why not use regex to search for a "
followed by any number of whitespace followed by a "
" followed by an optional comma?为什么不使用正则表达式来搜索
"
后跟任意数量的空格,后跟"
" 后跟可选的逗号?
const str = ` ".tagline-wrapper ":{ " padding-top":"398px", " font-size":" 1.8rem", " " }, ".tagline ":{ " padding-top":"398px", " font-size":" 1.8rem", " ", }, ".wrapper":{ " padding-top":"398px", " font-size":" 1.8rem", "" },`; console.log(str.replace(/"\s*",?/g, ''))
I clean your string and try to apply JSON.parse to convert it to an object.我清理了您的字符串并尝试应用 JSON.parse 将其转换为 object。
Noted : replaceAll is available in Nodejs 15.x.注意:replaceAll 在 Nodejs 15.x 中可用。
const str = `".tagline-wrapper ":{ " padding-top":"398px", " font-size":" 1.8rem", " " }, ".tagline ":{ " padding-top":"398px", " font-size":" 1.8rem", " ", }, ".wrapper":{ " padding-top":"398px", " font-size":" 1.8rem", "" },`; const cleanStr = "{" + str.replaceAll('" ', '"').replaceAll(' "', '"').replaceAll('""', "").replaceAll(",\n,", ",\n").replaceAll(",\n\n}", "}").replaceAll(",\n \n}", "}").slice(0, -1) + "}"; console.log(cleanStr); const object = JSON.parse(cleanStr); console.log(object);
Try this code it may help you试试这个代码它可以帮助你
function convertJSON(string) { string = string.replaceAll('"', "'"); string = string.replaceAll(/(\n\s*)/g, ''); string = string.replaceAll(/(,?\n?\s?'\s*',?)/g, ""); string = string.replaceAll(/},$/g, "}").replaceAll("'", '"'); let parsedJSON = null; try{ parsedJSON = JSON.parse(`{${string}}`); } catch{ console.log(string); } return parsedJSON; } let str = `".tagline-wrapper ":{ " padding-top":"398px", " font-size":" 1.8rem", " " }, ".tagline ":{ " padding-top":"398px", " font-size":" 1.8rem", " ", }, ".wrapper":{ " padding-top":"398px", " font-size":" 1.8rem", "" },`; console.log(convertJSON(str))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.