一、安装依赖:npm install pako
二、示例代码如下:
import pako from 'pako'
// 压缩JSON
function compressJSON(json) {
const jsonStr = JSON.stringify(json);
const compressed = pako.deflate(jsonStr);
return compressed;
}
// 解压JSON
function decompressJSON(compressed) {
const jsonStr = pako.inflate(compressed, { to: 'string' });
return JSON.parse(jsonStr);
}
// 示例JSON对象
const json = {
name: "John",
age: 30,
city: "New York"
};
// 压缩
const compressed = compressJSON(json);
console.log(compressed);
// 解压
const decompressed = decompressJSON(compressed);
console.log(decompressed);
代码图片