嘗試打印到控制臺日志從websocket提供的json數據中獲取一個值下面的代碼將所有的json數據從websocket打印到控制臺日志。// require wsconst WebSocket = require('ws');//messsage sent to ws servervar msg = {"jsonrpc": "2.0", "method": "public/subscribe", "id": 42, "params": { "channels": ["price_index.btc_usd"]} };// WS connection urlvar ws = new WebSocket('wss://website.com/ws/api/v2');//ws responsews.onmessage = function (e) { // do something with the notifications... console.log('server : ', e.data);};//stringify json dataws.onopen = function () { ws.send(JSON.stringify(msg));};預期結果:server : 5457.21server : 5457.19server : 5457.15實際結果:server : {"jsonrpc":"2.0","method":"subscription","params":{"channel":"deribit_price_index.btc_usd","data":{"timestamp":1556209117657,"price":5457.21,"index_name":"btc_usd"}}}server : {"jsonrpc":"2.0","method":"subscription","params":{"channel":"deribit_price_index.btc_usd","data":{"timestamp":1556209117657,"price":5457.19,"index_name":"btc_usd"}}}
從websocket過濾json對象/值并打印到控制臺日志
慕桂英546537
2021-05-01 10:07:39