用户可以从我们的应用程序下载JSON文件,我想对其进行美化,以便于调试。然而,即使缩进只是一个制表符或空格,这也会使文件大小增加近40%。
作为一种折衷方案,我想从美化中排除所有带有键"large“的值,如下所示:
{
"small":
{
"a": 1,
"b": 2,
"large": {"a":1,"b":2,"c":3,"d":"the \"large\" object should not be prettified"}
},
"large": {"a":1,"b":2,"c":3,"d":"the \"large\" object should not be prettified"}
}我尝试使用JSON.stringify的replacer parameter来解决这个问题:
JSON.stringify(data,(key,value)=>key==="large"?JSON.stringify(data):value,'\t');然而,“大”键的值最终被转义:
data =
{
"small":
{
"a": 1,
"b": [2,3,4,5],
"large": {"myarray":[1,2,3,4],"b":2,"c":3,"d":"the \"large\" object should not be prettified"}
},
"large": {"a":1,"b":2,"c":3,"d":"the \"large\" object should not be prettified"}
}
const json = JSON.stringify(data,(key,value)=>key==="large"?JSON.stringify(data):value,'\t');
document.getElementById("json").innerText = json;<html>
<body>
<pre id="json">
</pre>
</body>
</html>
如何防止这种转义发生,或者部分美化JavaScript中的JSON?
发布于 2020-10-06 17:00:17
每次在对象(或对象的一部分) more than once上调用JSON.stringify时,最终都会出现转义的语音标记。在这种情况下,内部JSON.stringify函数在replacer函数内部应用后,无法告知外部JSON.stringify函数不像对待任何其他字符串一样对待该实例。
所以,我认为如果你的目标是“部分”字符串一个对象,你需要写一个函数来实现它。对于这个问题,我建议这样做:
data = {
"small": {
"a": 1,
"b": [2, 3, 4, 5],
"c": [2, {"test":"example"}, [4,4], 5],
"large": {
"myarray": [1, 2, 3, 4],
"b": 2,
"c": 3,
"d": "the \"large\" object should not be prettified"
}
},
"large": {
"a": 1,
"b": 2,
"c": 3,
"d": "the \"large\" object should not be prettified"
}
}
function print(obj, exclude, space) {
let recur = (obj, spacing, inarray) => {
let txt = '';
if (inarray) {
if (Array.isArray(obj)) {
txt += '[';
for(let i=0;i<obj.length;i++) {
txt += recur(obj[i], spacing + space, true);
};
txt = txt.substr(0, Math.max(1,txt.length - 2)) + ']';
} else if (typeof obj === 'object' && obj !== null) {
txt += '{' + recur(obj, spacing + space, false) + '\n' + spacing + '}';
} else if (typeof obj === 'string') {
txt += obj.replaceAll(/\"/g, '\\"') + '"';
} else {
txt += obj;
};
return txt + ', ';
} else {
for (let key of Object.keys(obj)) {
if (exclude.indexOf(key) !== -1) {
txt += '\n' + spacing + '"' + key + '": ' + JSON.stringify(obj[key]);
} else if (Array.isArray(obj[key])) {
txt += '\n' + spacing + '"' + key + '": [';
for(let i=0;i<obj[key].length;i++) {
txt += recur(obj[key][i], spacing + space, true);
};
txt = txt.substr(0, Math.max(1,txt.length - 2)) + ']';
} else if (typeof obj[key] === 'object' && obj[key] !== null) {
txt += '\n' + spacing + '"' + key + '": {' + recur(obj[key], spacing + space, false) + '\n' + spacing + '}';
} else if (typeof obj[key] === 'string') {
txt += '\n' + spacing + '"' + key + '": "' + obj[key].replaceAll(/\"/g, '\\"') + '"';
} else {
txt += '\n' + spacing + '"' + key + '": ' + obj[key];
};
txt += ',';
};
return txt.substr(0, txt.length - 1);
};
};
return (Array.isArray(obj) ? '[' + recur(obj, space, true) + '\n' + ']' : '{' + recur(obj, space, false) + '\n' + '}');
};
document.getElementById("json").innerText = print(data, ['large'], '\t');<html>
<body>
<pre id="json">
</pre>
</body>
</html>
这里使用了一个递归函数,它遍历对象的键,并将键值对附加到字符串中,以处理不同的数据类型。如果在exclude数组中找到key,则会将其字符串化。
通过遍历密钥并在需要的地方有条件地应用JSON.stringify,您可以有效地避免上述问题。
我敢肯定,在自己编写JSON而不是使用内置函数时,可能会有一些边缘情况需要注意,但您可以根据自己的特定需求对其进行调整。
编辑:答案已更新,以便更好地考虑对象中的数组。
https://stackoverflow.com/questions/64152192
复制相似问题