Jquery consuming Web Service Rest

3

I would like to list the json's ceps, follow it:

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

And this is where I'm calling you:

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

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

                    });

I would like you to list all my json's ceps, how should I proceed?

    
asked by anonymous 30.10.2015 / 02:35

2 answers

0

You must scroll through the items in the array by picking up each cep from the objects. Following:

for( var index in data ) {

  alert( data[ index ].cep );

}
    
30.10.2015 / 03:02
2

A solution made with jquery using $.each to move through 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>
    
30.10.2015 / 03:28