我正在从websocket端点获取以下消息,并且想知道如何将消息充气并获取json。响应来自加密货币websocket api。我通常使用pako,但无法使pako在下面膨胀
回复
[ “78da6552cb8ac3300cfc17c39e3618c5b22dbbbf524a48936c6bfa2471d943e9bfafbd7152418f9a198d9e4fb10ffd2436dbadb0ca0180a844ad2428f4646b9523b1ab3249662619648b5e1a4f5a6bf08c33b31ca4331c9d338cf486bc43c5393d67e854dc32184b5d2b019df60a1d27df7ebc3135e728e018147b67c1794745bfab443b9dca06b25d1621cada594fc8264258fbb36955889a71f5624e8ea006eb38b76ea2b60eade35c694ae93c1cc79701d8045886454948356a438cd3508ad8345d5a2537d34b036c45582ea15102696335b9751f97763c0db1e9dbd88acd53748f711caeb199c2e12a36e23be97ec238c5e63e866e4888f1e511b262e89b3e4cb1bdfe53a8aa993ab72c61fdb4d36ddc87d82c15f6b15b35993e86c3f19d64ca079e6fbfac74b94afae24ffb74d837b8dc767a5c5248e9be52a577fa6cbbb977315b4ba42ff17afd018426bb16”]
谢谢
答案 0 :(得分:1)
要使用pako
,必须首先将数据从十六进制字符串转换为字节数组。这是执行此操作的一种方法:
function decodeHex(hexString) {
let result = new Uint8Array(hexString.length / 2);
for (let i = 0; i < result.length; i++) {
result[i] = parseInt(hexString.slice(2 * i, 2 * i + 2), 16);
}
return result;
}
let json = pako.inflate(decodeHex("your string"),
{ to: 'string' });
let decoded = JSON.parse(json);