我之前问过一个问题,这个问题都围绕着一个JSON格式问题。我有一个PHP页面,它只是从MYSQL数据库中获取行,并返回它们。但是,我不确定如何格式化它们所以我得到的所有列都是一个集合,并且对于每一行,它创建一个新集合。我不确定对这样的事情进行编码的常见做法是什么。
e.g。这就是我想要的(如果它是正确的):
{ "message": [
{
"chat":"Hello, world!",
"time":"2014-05-09 17:32:00",
"username":"Josue"
},
{
"chat":"This is my second message.",
"time":"2014-05-09 17:32:05",
"username":"Josue"
}
]
}
这样,我可以使用$ .parseAJAX解析,并访问我的数据,如:data.message[0].chat
,它将返回“Hello world!”。
以下是我目前正在做的事情:
$SQL = sprintf('SELECT time_sent, chat, username FROM Messages where time_sent >= date(\'%s\')', $last_chat_time);
$result = mysql_query($SQL);
$messages = Array();
while ( $row = mysql_fetch_assoc($result) ) {
$messages[] = 'chat';
$messages[] = $row['chat'];
$messages[] = 'time';
$messages[] = $row['time_sent'];
$messages[] = 'username';
$messages[] = $row['username'];
}
$loopCount = count($chats);
if(count($messages) > 0){
/*
$sexyJSON = '{message: [';
for($i = 0; $i < $loopCount; $i++){
$sexyJSON .= '{"chat":"'.$chats[$i].'","time":"'.$times[$i].'","username":"'.$usernames[$i].'"},';
}
$sexyJSON = substr($sexyJSON,0,strlen($sexyJSON)-1);
$sexyJSON .= ']}';
$newMessages = $sexyJSON;
echo $sexyJSON;
*/
echo json_encode($messages);
}
当我简单地编码我的数组时,它会返回如下内容:
["chat","Hello, world!","time","2014-05-09 17:32:00","username","Josue","chat","hmmm","time","2014-05-09 17:48:34","username","asdf"]
我需要做些什么才能将消息分组聊天,日期与日期以及用户名在键值对中?
答案 0 :(得分:3)
mysql_fetch_assoc的格式应为
array('chat'=>'Some chat', 'time_sent'=>'123456', 'username'=>'abcdefg')
json_encode会直接将其转换为
{"chat":"Some chat", "time_sent":"123456", "username":"abcdefg"}
所以在你的循环中,如果你只是按$mesages[] = $row;
并按原样保留你的json_encode,它应该如上所示工作。但是,您可以更改SQL语句以为列提供别名,以便time_sent
只显示属性time
答案 1 :(得分:2)
这就是我要做的事情:
$SQL = sprintf('SELECT time_sent, chat, username FROM Messages where time_sent >= date(\'%s\')', $last_chat_time);
$result = mysql_query($SQL);
$messages = array();
while ( $row = mysql_fetch_assoc($result) ) {
$arr = array();
$arr['chat'] = $row['chat'];
$arr['time'] = $row['time_sent'];
$arr['username'] = $row['username'];
$messages[] = $arr;
}
$loopCount = count($chats);
if(count($messages) > 0){
echo json_encode($messages);
}
如果没有编码,将输出:
Array
(
[0] => Array
(
[chat] => chat_0
[time] => time_sent_0
[username] => username_0
)
[1] => Array
(
[chat] => chat_1
[time] => time_sent_1
[username] => username_1
)
[2] => Array
(
[chat] => chat_2
[time] => time_sent_2
[username] => username_2
)
[3] => Array
(
[chat] => chat_3
[time] => time_sent_3
[username] => username_3
)
)
如果编码:
[{"chat":"chat_0","time":"time_sent_0","username":"username_0"},
{"chat":"chat_1","time":"time_sent_1","username":"username_1"},
{"chat":"chat_2","time":"time_sent_2","username":"username_2"},
{"chat":"chat_3","time":"time_sent_3","username":"username_3"}]
解析JSON
假设您将JSON结果放在data
var
var obj = $.parseJSON(data);
$.each(obj, function(i, value){
console.log(value.chat);
});
答案 2 :(得分:1)
您需要使用多维数组才能使其正常工作。下面的代码已经过编辑,可以为命名索引赋值,并将这些值附加到$ messages数组的第二级。
$messages = Array();
while ( $row = mysql_fetch_assoc($result) ) {
$messages[] = Array(
'chat' => $row['chat'],
'time' => $row['time_sent'],
'username' => $row['username']
);
}
答案 3 :(得分:1)
while循环应该是这样的:
while ( $row = mysql_fetch_assoc($result) ) {
$arr = array();
$arr['chat'] = $row['chat'];
$arr['time'] = $row['time_sent'];
$arr['username'] = $row['username'];
$messages[] = $arr;
}
答案 4 :(得分:0)
通过在SQL中获取字段名称来节省一点时间。
$SQL = sprintf('SELECT time_sent as `time`, chat, username FROM Messages where time_sent >= date(\'%s\')', $last_chat_time);
$result = mysql_query($SQL);
$messages = array();
while ($row = mysql_fetch_assoc($result)) {
$messages[] = $row;
}
echo json_encode($messages);
IMO在没有消息的情况下返回一个空数组比返回任何消息要好,因为现在你可以在JavaScript代码中区分你的php是否有效,如果你没有消息。