如何使用每一個解析JSON類型 -jQuery的 - 解析JSON - 2個陣列
retrieveMessage: function()
{
$.post('ActionScripts/RetrieveMessage.php',{
}, function(data) {
$.each(data, function(key, reader){
alert(reader.id + reader.count); //this line does not return anything
});
},"json");
}
數據:
[{"id":"1","user_name":"Jenan","user_image":"ImageName","text_chat":"Hello","date":"2011-09-25 21:29:09","error":""}][{"count":"2"}]
我會得到的文本 -
id=1 count=2
PHP
$jsonresult = array();
$count = array();
$countresult = array();
$countresult["count"] = "2";
$count[] = $countresult;
while($row = mysql_fetch_array($result))
{
$thisResult = array();
$thisResult["user_auth"] = 1;
$thisResult["id"] = $row['id'];
$thisResult["user_name"] = $row['user_name'];
$thisResult["user_image"] = $row['user_image'];
$thisResult["text_chat"] = $row['text_chat'];
$thisResult["date"] = $row['date'];
$thisResult["error"] = "";
$jsonresult[] = $thisResult;
}
echo json_encode($jsonresult) . json_encode($count);
這裏組合兩個藏品。 如何讀取具有id參數和收集計數的集合?這是正確的解決方案嗎?我選擇哪種解決方案來組成陣列?
感謝您的意見。
這不是有效的JSON,你不能只有一個接一個地接下來的兩個數組。你有權修改返回的JSON嗎? –
@kingjiv,它是有效的JSON,但它是廢話。您可以在數組的每個索引中擁有不同的結構化元素,但這實際上表明實現服務器部分的人的設計非常糟糕。 –
@DarinDimitrov:我不同意。將它粘貼到http://jsonlint.com/它只是2個數組相鄰,而不是1個對象或數組。 –