JavaScript 压缩 JSON 函数
以下是使用 JavaScript 实现压缩 JSON 的示例代码:
function compressJSON(json) {
return JSON.stringify(json).replace(/\s+/g, '');
}
// 示例
var json = { 'name': 'John', 'age': 30 };
var compressedJson = compressJSON(json);
console.log(compressedJson);
输出结果:
{'name':'John','age':30}
该函数使用 JSON.stringify 将 JSON 转换为字符串,并使用正则表达式替换所有空格字符。最后返回压缩后的 JSON 字符串。
原文地址: https://www.cveoy.top/t/topic/ntxi 著作权归作者所有。请勿转载和采集!