JSON - 访问值和循环

时间:2012-09-25 06:09:35

标签: javascript jquery json

在表单提交上我正在向PHP代码发出AJAX请求,作为回应,这就是我得到的。

var data = {
    "empty":{
        "game_sais_no":"Season cannot contain empty value",
        "game_sc_no":"Category cannot contain empty value",
        "game_st_no2":"Visiting team connot contain empty value",
        "game_room_no_2":"Visiting room cannot contain empty value",
        "game_room_no_1":"Local chamber cannot contain empty value",
        "game_date":"Game date should be specified",
        "game_time":"Game time should be specified",
        "game_time_start":"Game start time should be specified",
        "game_time_close":"Game close time should be specified",
        "game_place_no":"Arena \/ Lot should be specified",
        "game_status":"Game status should be specified"
    }
} 

1 即可。我想访问单个值。我试着像这样访问它。

data.empty.game_sais_no it returns me the value of undefined.

2 即可。我想循环遍历json对象并将所有消息显示回用户。我尝试使用

$.each(data, function(index, value)(){
     //build the markup.
});

这给了我意想不到的结果。我哪里错了?

更新: 我不确定但由于某种原因它给了我奇怪的结果,让我告诉你我到底在做什么。

这是我对php的ajax调用。

$('#gce_game_btn').on('click', function(){
    var formData = $('#gce_game_form').serialize();
    $.ajax({
        type    : 'POST',
        url     : 'accueil.php?m=ajax&game=1',
        data    : formData,
        success : function(data) {
            //
        }
    });
});

这是我要发回的数组。

Array
(
    [empty] => Array
        (
            [game_sais_no] => Season cannot contain empty value
            [game_sc_no] => Category cannot contain empty value
            [game_st_no2] => Visiting team connot contain empty value
            [game_room_no_2] => Visiting room cannot contain empty value
            [game_room_no_1] => Local chamber cannot contain empty value
            [game_date] => Game date should be specified
            [game_time] => Game time should be specified
            [game_time_start] => Game start time should be specified
            [game_time_close] => Game close time should be specified
            [game_place_no] => Arena / Lot should be specified
            [game_status] => Game status should be specified
        )

)

我正在使用json_encode()并回复它。这反过来又把它作为字符串。

{
    "empty":{
        "game_sais_no":"Season cannot contain empty value",
        "game_sc_no":"Category cannot contain empty value",
        "game_st_no2":"Visiting team connot contain empty value",
        "game_room_no_2":"Visiting room cannot contain empty value",
        "game_room_no_1":"Local chamber cannot contain empty value",
        "game_date":"Game date should be specified",
        "game_time":"Game time should be specified",
        "game_time_start":"Game start time should be specified",
        "game_time_close":"Game close time should be specified",
        "game_place_no":"Arena \/ Lot should be specified",
        "game_status":"Game status should be specified"
    }
} 

3 个答案:

答案 0 :(得分:1)

首先,您不会在数组中返回响应。它应该看起来像。请参阅[]

"empty":[{
        "game_sais_no":"Season cannot contain empty value",
        "game_sc_no":"Category cannot contain empty value",
        "game_st_no2":"Visiting team connot contain empty value",
        "game_room_no_2":"Visiting room cannot contain empty value",
        "game_room_no_1":"Local chamber cannot contain empty value",
        "game_date":"Game date should be specified",
        "game_time":"Game time should be specified",
        "game_time_start":"Game start time should be specified",
        "game_time_close":"Game close time should be specified",
        "game_place_no":"Arena \/ Lot should be specified",
        "game_status":"Game status should be specified"
    }]

然后你会以

的形式阅读
$.each(response.empty, function(index) {
           alert(response.empty[index].game_sais_no);

        });

答案 1 :(得分:1)

工作正常。 Check the demo

$.each(data, function(index, value){
    console.log(index);
    $.each(value, function(index, value) {
       console.log(index, value);
    });
});
​

答案 2 :(得分:1)

您是否在浏览器的控制台部分看到任何错误? 您尝试访问json对象的方式没有错误

试试这个

$.each(data.empty, function(i,value){
        console.log(value);
    }) ; 

检查FIDDLE

<强>已更新

您似乎错过了ajax请求中的 dataType:'json'属性。 如果您没有指定它将数据解析为字符串

$.ajax({
        type    : 'POST',
        url     : 'accueil.php?m=ajax&game=1',
        data    : formData,
        dataType: 'json'
        success : function(data) {
            //
        }
    });