这是一个很难解释的问题,所以请告诉您.如果您在下面看,您将看到有效的JSON.
{
"data":{
"0":{
"action_id":"1",
"date":"2012-04-10 15:07:38",
"action_type":"1",
"action_text":"Some one got blamed!"
},
"1":{
"action_id":"2",
"date":"2012-04-10 16:18:05",
"action_type":"1",
"action_text":"Testing multiple items for AJAX"
},
"total":2,
"ajax_message":"Success",
"ajax_status":"0",
"success":"true"
}
}
但是对于正在使用的应用程序,它无法处理“ 0”:,“ 1”,而是只希望将其逗号分隔.
我当前生成的代码是:
while ($r = mysql_fetch_assoc($q)) {
$array[] = $r;
}
json_encode($array);
目前还相当简单和原始.但是我想我可能不得不为自己写一个json_encode,以便它像这样打印它…..
任何帮助将不胜感激
注意:这是一个有效的表格(手写):
{
"data": [
{
"action_id": "1",
"date": "2012-04-10 15:07:38",
"action_type": "1",
"action_text": "Some one got blamed!",
"fb_id": "760775384"
},
{
"action_id": "2",
"date": "2012-04-10 16:18:05",
"action_type": "1",
"action_text": "Testing multiple items for AJAX",
"fb_id": "760775384"
}
],
"total": 2,
"ajax_message": "Success",
"ajax_status": "0",
"success": "true"
}
解决方法:
问题在于您的数字键(0、1)在哈希中与命名键(总计,ajax_message等)处于同一级别.而不是这样做:
$a = array();
while ($r = mysql_fetch_assoc($q)){
$a[] = $r;
}
$a['key'] = value;
做这个:
$a = array();
$a['rows'] = array();
while ($r = mysql_fetch_assoc($q)){
$a['rows'][] = $r;
}
$a['key'] = value;
如果数组中的每个键(在此示例中为$a [‘rows’])都是数字键,则json_encode()会将其输出为[{list},{like},{this}]