Javascript Ajax和JSON数组

Javascript Ajax和JSON数组,javascript,php,jquery,ajax,json,Javascript,Php,Jquery,Ajax,Json,我正在尝试将JSON数据格式与jQuery结合使用,如果它能够工作,那将是令人惊讶的 在我的remoteServiceEngine.php中,我有如下内容: $jResponse[0] = json_encode(array("jAction" => "add", "jObject" => "listCountries", "jBody" => "test")); $json_data = json_encode(array("jRequestState" => $jRe

我正在尝试将JSON数据格式与jQuery结合使用,如果它能够工作,那将是令人惊讶的

在我的remoteServiceEngine.php中,我有如下内容:

$jResponse[0] = json_encode(array("jAction" => "add", "jObject" => "listCountries", "jBody" => "test"));
$json_data = json_encode(array("jRequestState" => $jRequestState, "jMessage" => $jMessage, "jResponse" => $jResponse));
echo $json_data;
这就是JS中的处理方式:

 success: function(remoteResponse){
        switch(remoteResponse.jRequestState) {
            case 0:
                $("#removeServiceMessage").fadeIn(2000).html('<div class="remoteError">'+remoteResponse.jMessage+'</div>').fadeOut(2000);
            break;

            case 1:
                $("#removeServiceMessage").fadeIn(2000).html('<div class="remoteSuccess"><B>Success:</B> '+remoteResponse.jMessage+'</div>').fadeOut(2000);
                for (i = 0; i < remoteResponse.jResponse.length; i++) {
                    switch(remoteResponse.jResponse[i].jAction) {
                        case "add":
                            $("#"+remoteResponse.jResponse[i].jObject).fadeIn(1000).append(remoteResponse.jResponse[i].jBody);
                        break;

                        case "remove":
                            $("#"+remoteResponse.jResponse[i].jObject).fadeOut(1000);
                        break;

                        case "update":
                            $("#"+remoteResponse.jResponse[i].jObject).fadeIn(1000).html(remoteResponse.jResponse[i].jBody);
                        break;

                        default:
                            alert(remoteResponse.jResponse[i]);
                        break;
                    }
                }
            break;
        }
    }
但我不知道如何访问这些元素。我试了一下:

alert(remoteResponse.jResponse[i]['jAction']);


因为我从未在jQuery中使用JSON,所以我不知道如何处理它。有人帮忙吗?

就像adeneo在评论中说的那样,你需要两次编码。您的php代码应该如下所示:

$jResponse[0] = array("jAction" => "add", "jObject" => "listCountries", "jBody" => "test");
$json_data = json_encode(array("jRequestState" => $jRequestState, "jMessage" => $jMessage, "jResponse" => $jResponse));
echo $json_data;
您在PHP中将$jResponse[0]两次编码为JSON。您可能想了解一下,因为这可以通过只处理数据并将其绑定到适当的控件来减少jQuery的使用,如果您感兴趣,请回复消息,我将为您发布一个淘汰答案:
alert(remoteResponse.jResponse[i][0]); //yeah, that's kinda stupid solution, but well...
$jResponse[0] = array("jAction" => "add", "jObject" => "listCountries", "jBody" => "test");
$json_data = json_encode(array("jRequestState" => $jRequestState, "jMessage" => $jMessage, "jResponse" => $jResponse));
echo $json_data;