[英]How to prettify JSON.stringify() partially?
用户可以从我们的应用程序下载 JSON 文件,我想对其进行美化,以便比所有内容都在一行中更容易调试。 然而,即使缩进只是单个制表符或空格,这也会使文件大小增加近 40%。
作为妥协,我想从美化中排除所有具有例如“大”键的值,如下所示:
{
"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 参数解决这个问题:
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?
每次在对象(或对象的一部分)上多次调用JSON.stringify
时,最终都会得到转义的语音标记。 在这种情况下,在JSON.stringify
函数内部应用了内部JSON.stringify
函数之后,无法告诉外部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 而不是使用内置函数时,可能会有一些边缘情况需要注意,但您可以调整这样的内容以满足您的特定需求。
编辑:答案更新以更好地考虑对象内的数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.