-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 개의 배열입니다. –