如何设置和格式化json响应

时间:2018-12-07 19:05:26

标签: javascript html json websocket

<!doctype html>

<html>

<head>

<script src="https://code.jquery.com/jquery-1.11.0.min.js"></script>

<script src="https://code.jquery.com/jquery-2.2.3.js"></script>

</head>

<body>

<div id=“output”>

<script>

    const pricesWs = new WebSocket('wss://ws.coincap.io/prices? 
    assets=bitcoin,ethereum,monero,litecoin');

    pricesWs.onmessage = function (msg) {

     console.log(msg.data);

     var tmpData = JSON.parse(msg.data);

 var formattedData = JSON.stringify(tmpData, null, '\t');

 $('#output').text(formattedData);
    };

</script>

</body>

</html>

上面的代码在浏览器上显示这样的json响应。

{  
   "bitcoin":"6389.06534240",
   "ethereum":"192.93111286",
   "monero":"108.90302506",
   "litecoin":"52.25484165"
}

但是我希望能够对响应的样式和格式设置为真实的html,我的意思是我希望响应看起来像这样的https // CoinCap.io

0 个答案:

没有答案