js去掉json字符串key的双引号
作者:互联网
代码示例
let jsonStr=`{
"keyWord": "",
"page": {
"pages": 1,
"records": [{
"readFlag": 0
}],
"size": 10,
"a":null,
"b":true,
"c":[],
"d":{}
}
}`
function addMarks(jsonObj) {
if (Array.isArray(jsonObj)) {
for (const item of jsonObj) {
if (typeof item==='object'&&item) {
addMarks(item)
}
}
}else{
for (const key in jsonObj) {
if (Object.hasOwnProperty.call(jsonObj, key)) {
jsonObj['<'+key+'>']=jsonObj[key]
if (typeof jsonObj[key]==='object' && jsonObj[key]) {
addMarks(jsonObj[key])
}
delete jsonObj[key]
}
}
}
}
function delQuotes(jsonStr) {
let jsonObj=JSON.parse(jsonStr)
addMarks(jsonObj)
return JSON.stringify(jsonObj).replace(/"<|>"/g,"")
}
console.log(delQuotes(jsonStr))
结果
{
keyWord: "",
page: {
pages: 1,
records: [{
readFlag: 0
}],
size: 10,
a: null,
b: true,
c: [],
d: {}
}
}
标签:jsonStr,jsonObj,&&,js,item,json,key,addMarks 来源: https://blog.csdn.net/ywy502/article/details/120783487