-3

I am getting JSON encoded data after ajax call to PHP :

[  
   {  
      "id":"4",
      "name":"Kg",
      "vital_sign_list_id":"3",
      "created":"2016-03-01 18:52:27",
      "modiefied":"2016-03-01 18:52:27"
   },
   {  
      "id":"5",
      "name":"Pound",
      "vital_sign_list_id":"3",
      "created":"2016-03-01 18:52:27",
      "modiefied":"2016-03-01 18:52:27"
   }
]

There are two datas, and I want to decode using loop and use data using jquery. How can I achieve it?

Moumit
  • 8,314
  • 9
  • 55
  • 59
Asmita
  • 171
  • 1
  • 11

3 Answers3

1

try below code:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-beta1/jquery.js"></script>
<script>
    json = '[{"id":"4","name":"Kg","vital_sign_list_id":"3","created":"2016-03-01 18:52:27","modiefied":"2016-03-01 18:52:27"}, {"id":"5","name":"Pound","vital_sign_list_id":"3","created":"2016-03-01 18:52:27","modiefied":"2016-03-01 18:52:27"}]';
    var obj = $.parseJSON( json);
    $.each(obj, function(index, val){
       alert(val.name);
       //you can access other data by using val.id, val.created etc
    });
</script>

for more detail have a look at http://api.jquery.com/jquery.parsejson/ and http://api.jquery.com/jquery.each/

Chetan Ameta
  • 7,696
  • 3
  • 29
  • 44
1

You can try this way

var data = [{"id":"4","name":"Kg","vital_sign_list_id":"3","created":"2016-03-01 18:52:27","modiefied":"2016-03-01 18:52:27"}, {"id":"5","name":"Pound","vital_sign_list_id":"3","created":"2016-03-01 18:52:27","modiefied":"2016-03-01 18:52:27"}];



for(var i = 0 ; i < data.length; i++){
  
  console.log("ID "+ i +": "+ data[i].id);
  console.log("Name "+ i +": "+ data[i].name);
  console.log("Vital "+ i +": "+ data[i].vital_sign_list_id);
  }
guradio
  • 15,524
  • 4
  • 36
  • 57
0
json_encode(jsonArrVar);

use this in your json php file to encode your json data. and in jquery parse it using json.parse or eval function.