Filipe Machado Carneiro Filipe Machado Carneiro - 27 days ago 10
Javascript Question

Trying to parse a json with several arrays inside

This is my Index.html

<div id="#here">

</div>

<script type="text/javascript">
$( document ).ready(function() {

$.getJSON("https://poloniex.com/public?command=returnOrderBook&currencyPair=BTC_ETH&depth=30", function(data){
var x = data.asks
$.each(x, function(index, item) {
//console.log(item);
$('#here').append("<p>"+ item +"</p>");
});
});
});
</script>


i'm just trying to parse a json an put inside a
p
.

in the console the script runs ok

Printscreen

help guys, pleas

Answer

item is an array so you need to access the array element using brackets notation like so:

item[0]

You also want to remove the # symbol from the id attribute of your div element. The id attribute value must begin with a letter.

So this should work.

--HTML--

<div id="here"></div>

--JS--

$( document ).ready(function() {

  $.getJSON("https://poloniex.com/public?command=returnOrderBook&currencyPair=BTC_ETH&depth=30", function(data) {
  var x = data.asks;

  $.each(x, function(index, item) {

    var $paragraph = $('<p>');
    $paragraph.text(item[0])
    $('#here').append($paragraph);

    });

  });

});