Jquery consuming Web Service Rest

Question:

I would like to list this json's zip codes, as follows:

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

And this is where I'm calling:

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

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

                    });

I would like them to list all the zip codes of my json, how should I proceed?

Answer:

A jquery solution using $.each to iterate 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>
Scroll to Top