我已经为JSON文件中的某些对象获取了未定义的消息,即使它们不是空的。可以解析并且工作正常的对象包括model_no,likes,dislikes,average_min,average_max和average_users。但是其他对象(如user_id,ulike_type,uaverage_min等)在Chrome控制台中显示为未定义,即使它们在文件中有值。任何人都可以告诉我JSON编码有什么问题吗?
这是我的ajax代码:
var number = $('input').map(function(){
return $(this).attr('rel')
}).get();
$.ajax({
url: "file.php",
type: "POST",
dataType:"JSON",
data:{'group[]':number},
success: function (data) {
var item_html = "",
$.each(data, function(i,item){
console.log(item.dislikes); // this one shows value
console.log(item.users_id); // undefined
console.log(item.min) // undefined
console.log(item.max) // undefined
})
}
});
PHP文件:
<?php
if($_POST['group'])
{
header("Content-Type: application/json");
$callback = $_GET['callback'];
require_once 'config.php';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=$databasename", $username, $password);
$id_group = $_POST["group"];
$in = str_repeat('?,', count($id_group) - 1) . '?';
$sql = "
SELECT * FROM
(
SELECT COUNT( c.app_id ) AS users_no, ROUND( AVG( c.min ) , 1 ) AS avg_min, ROUND( AVG( c.max ) , 1 ) AS avg_max, a.mid, a.likes, a.dislikes, b.model_no
FROM `likes_total` a
RIGHT JOIN `product_info` b ON a.mid = b.mid
LEFT JOIN `resale` c ON c.mid = b.mid
WHERE b.model_no IN ($in)
GROUP BY b.model_no
)TAB1
JOIN
(
SELECT a.app_id,b.model_no,c.isbooked,d.min,d.max,e.like_type
FROM `users_info` a
JOIN `product_info` b
ON b.model_no IN ($in)
LEFT JOIN `bookmarked` c
ON c.app_id = a.id
AND c.mid = b.mid
LEFT JOIN `resale` d
ON d.app_id = a.id
AND d.mid = b.mid
LEFT JOIN `likes_count` e
ON e.app_id = a.id
AND e.mid = b.mid
WHERE a.app_id = ?
)TAB2
ON TAB1.model_no = TAB2.model_no
";
$users = $dbh->prepare($sql);
$i = 1;
foreach ($id_group as $id) {
$users->bindValue($i++, $id);
}
$results = array_merge($id_group,$id_group);
$lasti = (count($results) + 1);
$user_id = $_SESSION["user_id"];
$users->bindValue($lasti,$user_id);
$mergearray = array_merge($results,array($user_id));
$users->execute($mergearray);
$datas = array();
$rows = $users->fetchAll(PDO::FETCH_ASSOC);
foreach($rows as $row )
{
$data = array(
"user_id"=> $row["app_id"],
"ulike_type"=> $row["like_type"],
"uaverage_min"=> $row["min"],
"uaverage_max"=> $row["max"],
"ufav"=> $row["isbooked"],
"model_no"=> $row["model_no"],
"likes"=> $row["likes"],
"dislikes"=>$row["dislikes"],
"average_min"=> $row["avg_min"],
"average_max"=> $row["avg_max"],
"average_users"=> $row["users_no"],
);
$datas[] = $data;
}
echo ($callback ? $callback . '(' : '') . json_encode($datas) . ($callback ? ')' : '');
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
?>
JSON输出示例:
[
{
"user_id": "111",
"ulike_type": null,
"uaverage_min": null,
"uaverage_max": null,
"ufav": null,
"model_no": "AAAA",
"likes": "0",
"dislikes": "0",
"average_min": null,
"average_max": null,
"average_users": "0"
},
{
"user_id": "222",
"ulike_type": "0",
"uaverage_min": "398",
"uaverage_max": "823",
"ufav": null,
"model_no": "BBB",
"likes": "0",
"dislikes": "1",
"average_min": "398.0",
"average_max": "823.0",
"average_users": "1"
}
]
Print_r($ datas)的输出
Array
(
[0] => Array
(
[user_id] => 111
[ulike_type] =>
[uaverage_min] =>
[uaverage_max] =>
[ufav] =>
[model_no] => AAAA
[likes] => 0
[dislikes] => 0
[average_min] =>
[average_max] =>
[average_users] => 0
)
[1] => Array
(
[user_id] => 222
[ulike_type] => 0
[uaverage_min] => 398
[uaverage_max] => 823
[ufav] =>
[model_no] => BBB
[likes] => 0
[dislikes] => 1
[average_min] => 398.0
[average_max] => 823.0
[average_users] => 1
)
)
答案 0 :(得分:2)
您将其定义为average_users
。
键值对的左侧是您在右侧值旁边的名称。
答案 1 :(得分:2)
item.users_id
应为item.user_id
,item.min
应为item.average_min
,item.max
应为item.average_max
。
答案 2 :(得分:1)
在Javascript中你有:
item.users_id
但是在JSON中你有:
"user_id": "111"
(用户,而不是用户)