Jquery consuming Web Service Rest

Asked

Viewed 763 times

3

I would like to list the ceps of this json, follows him:

 [
  {
    "id": 1,
    "nome": "Hospital Da Mulher",
    "cep": "60508090"
  },
  {
    "id": 2,
    "nome": "Hospital Maria jose",
    "cep": "2"
  }
]

And that’s where I’m calling:

 $.getJSON('/MinhaDoenca/rest/hospital/get',
                    function(data) {

                        alert("O cep é:  " + data.cep);

                    });

I would like you to list all the ceps of my json, how should I proceed?

2 answers

2

A solution made with jquery using the $.each to traverse the object.

Example:

var dados = [{
  "id": 1,
  "nome": "Hospital Da Mulher",
  "cep": "60508090"
}, {
  "id": 2,
  "nome": "Hospital Maria jose",
  "cep": "2"
}];

$.each(dados, function(key, val) {
  alert('id=' + val.id + ' nome=' + val.nome + ' cep=' + val.cep);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

0


You must scroll through the items of the array by grabbing each cep of the objects. Next:

for( var index in data ) {

  alert( data[ index ].cep );

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.