Utilizzo di JQuery AJAX e php per recuperare i dati da adatabase mysql c'è già un esempio ma Non riesco a capire come gestire l'array di oggetti restituito. Di seguito è riportato il mio output json dopo l'esecuzione di api.php:
[{"timestamp":"12","gas_use":"3000.000","elec_use":"40000.000"},
{"timestamp":"34","gas_use":"4000.000","elec_use":"5000.000"},
{"timestamp":"56","gas_use":"1000.000","elec_use":"2000.000"}]
La mia domanda è come gestisco l'array di dati nel seguente client.php
$(function ()
{
//-------------------------------------------------------------------------------------------
// 2) Send a http request with AJAX http://api.jquery.com/jQuery.ajax/
//-------------------------------------------------------------------------------------------
$.ajax({
url: "api.php", //the script to call to get data
data: "",
dataType: "json", //data format
success: function (data) {
},
}
Qualsiasi aiuto sarebbe apprezzato.
risposte:
0 per risposta № 1for(var i in data){
var obj = data[i];
// obj.timestamp;
// obj.gas_use;
// etc
}
0 per risposta № 2
It will work:
var $jsonData = [{"timestamp":"12","gas_use":"3000.000","elec_use":"40000.000"},
{"timestamp":"34","gas_use":"4000.000","elec_use":"5000.000"},
{"timestamp":"56","gas_use":"1000.000","elec_use":"2000.000"}];
$.each( $jsonData, function(key, data){
// alert(data.timestamp);
// alert(data.gas_use);
// alert(data.elec_use);
});