1

I'm trying to parse JSON data from an external URL I have managed to print the data but I'm having difficulties looping it and printing the complete data

here's my code

<div class="coin"></div>

    <script>
    $.getJSON('https://www.binance.com/gateway-api/v1/public/indicator/abnormal-trading-notice/list', function(fetch) {
        
        var coin = `noticeType: ${fetch.data[0].noticeType}<br>
                    symbol: ${fetch.data[0].symbol}<br>
                    eventType: ${fetch.data[0].eventType}<br>
                    volume: ${fetch.data[0].volume}<br>
                    priceChange: ${fetch.data[0].priceChange}<br>
                    period: ${fetch.data[0].period}<br>
                    createTimestamp: ${fetch.data[0].createTimestamp}<br>
                    updateTimestamp: ${fetch.data[0].updateTimestamp}<br>
                    baseAsset: ${fetch.data[0].baseAsset}<br>
                    quotaAsset: ${fetch.data[0].quotaAsset}<br>`
        $(".coin").html(coin);
    });
    </script>
neo81040
  • 23
  • 6
  • 1
    You printed `fetch.data[0]` into `.coin`, but it's not clear where you would like to print the other elements of the `fetch.data` array. – kol Feb 13 '21 at 20:06

1 Answers1

2

Try this:

<script>

$.getJSON('https://www.binance.com/gateway-api/v1/public/indicator/abnormal-trading-notice/list', function (fetch) {

    for (var i = 0; i < fetch.data.length; i++) {
        $(".coin").append(`noticeType: ${fetch.data[i].noticeType}<br>
                symbol: ${fetch.data[i].symbol}<br>
                eventType: ${fetch.data[i].eventType}<br>
                volume: ${fetch.data[i].volume}<br>
                priceChange: ${fetch.data[i].priceChange}<br>
                period: ${fetch.data[i].period}<br>
                createTimestamp: ${fetch.data[i].createTimestamp}<br>
                updateTimestamp: ${fetch.data[i].updateTimestamp}<br>
                baseAsset: ${fetch.data[i].baseAsset}<br>
                quotaAsset: ${fetch.data[i].quotaAsset}<br> <br/><br/>`);
    }
});
Arib Yousuf
  • 1,230
  • 1
  • 8
  • 12
  • Is there a way I can request this data every 1 second automatically? – neo81040 Feb 13 '21 at 20:27
  • @neo81040 yes, see `setInterval` https://www.w3schools.com/js/js_timing.asp – Risinek Feb 13 '21 at 20:29
  • Yes, you can use the javascript "setInterval" function. Please follow the link. https://stackoverflow.com/questions/2170923/whats-the-easiest-way-to-call-a-function-every-5-seconds-in-jquery – Arib Yousuf Feb 13 '21 at 20:31
  • Fetching the data every second seems a bit too frequent... Please be careful not to overload the API with your requests... – kol Feb 13 '21 at 20:33