[英]json object returning 'undefined" values along with actual values
So, I've looked through similar questions out here, but still couldn't find an answer to my query. 因此,我在这里浏览了类似的问题,但仍然找不到我的查询的答案。 For some reason, I seem to get "undefined values along with my actual values in my json object. I receive these values from my php file. The code ffor the php file is as follows:
由于某种原因,我似乎在json对象中获得了“未定义的值以及实际值。我从php文件中收到这些值。php文件的代码f如下:
$sql = "SELECT distinct rackID FROM rackusage where startTime like '%".$curdate."%'";
$res = mysql_query($sql);
while($r = mysql_fetch_assoc($res)){
// Queries to get respective values to put inside my array
$temp[] = array('id' => (int) $r['rackID']);
$temp[] = array('rackName' => (string) $rackname);
$temp[] = array('uptime' => (int) $uptime);
$temp[] = array('downtime' => (int) $downtime);
$temp[] = array('utilization' => (int) $utilization);
} echo json_encode($temp);
The output string is as follows: 输出字符串如下:
[{"id":1},{"rackName":"xyz"},{"uptime":119},{"downtime":0},{"utilization":0},{"id":2},{"rackName":"abc"},{"uptime":122},{"downtime":0},{"utilization":0},{"id":3},{"rackName":"pqr"},{"uptime":114},{"downtime":0},{"utilization":0}]
[{ “ID”:1},{ “rackName”: “XYZ”},{ “正常运行时间”:119},{ “停机时间”:0},{ “利用”:0},{ “ID”:2} ,{ “rackName”: “ABC”},{ “正常运行时间”:122},{ “停机时间”:0},{ “利用”:0},{ “ID”:3},{ “rackName”:“PQR “},{” 正常运行时间 “:114},{” 停机时间 “:0},{” 利用“:0}]
The JS code is: JS代码是:
function workwithdate(curdate){
$.ajax({
type: "Post",
url: 'senddata.php',
data:{'selectDate': curdate},
async: false,
success: function(data){
if(data){
alert(data);
recData= $.parseJSON(data);
//recData = data;
console.log(recData);
alert(recData);
alert(recData.length);
$.each(recData, function(idx, la){
var someid = la.id;
console.log(someid);
var rName = la.rackName;
console.log(rName);
var rUptime = la.uptime;
console.log(rUptime);
});
}
}
});
}
My output looks like this: 我的输出如下所示:
TIA TIA
You want one associative array per row, so: 您需要每行一个关联数组,因此:
while($r = mysql_fetch_assoc($res)){
$temp[] = array(
'id' => (int) $r['rackID'],
'rackName' => (string) $rackname,
'uptime' => (int) $uptime,
'downtime' => (int) $downtime,
'utilization' => (int) $utilization
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.