我在调用PHP后调用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"
}
]
有两个数据,我想使用循环解码并使用 jquery 来使用数据。我怎样才能实现它?
答案 0 :(得分:1)
尝试以下代码:
<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>
有关详细信息,请查看http://api.jquery.com/jquery.parsejson/和http://api.jquery.com/jquery.each/
答案 1 :(得分:1)
你可以试试这种方式
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);
}
&#13;
答案 2 :(得分:0)
json_encode(jsonArrVar);
在你的json php文件中使用它来编码你的json数据。 并在jquery中使用json.parse或eval函数解析它。