json编码多维数组获取值

时间:2015-01-17 03:49:38

标签: javascript php arrays json multidimensional-array

 Array
(
    [0] => Array
        (
            [sysid] => 1
            [code] => 140101000
            [name] => China
            [parentid] => 1
        )

    [1] => Array
        (
            [sysid] => 2
            [code] => 140102000
            [name] => Japan
            [parentid] => 1
        )

    [2] => Array
        (
            [sysid] => 3
            [code] => 140103000
            [name] => Hongkong
            [parentid] => 1
        )
)

这是我的数组,我从我的print_r请求得到它来自php它是一个多维数组,我使用json_encode现在这是我从成功获得的数据。我想从这个json中获取sysid和name的所有值,以便将它放入select选项中这是怎么可能的

我在下面使用了这段代码,但我得到了未定义的内容,我获得了4,000个结果

PAIR 0: undefined

PAIR 0: undefined

PAIR 1: undefined

PAIR 1: undefined

for (var i = 0; i < data.length; i++) {
    console.log("PAIR " + i + ": " + data[i].sysid);
    console.log("PAIR " + i + ": " + data[i].name);
}

UPDATE

我的不好第一个是print_r,这是json

[{"sysid":"1","code":"140101000","name":"China","parentid":"1"},{"sysid":"2","code":"140102000","name":"Japan","parentid":"1"},
{"sysid":"3","code":"140103000","name":"Hongkong","parentid":"1"}]

ajax是

$.ajax({
    type: 'POST',
    url: '../include/country.php',
    data: {
        id: id
    },
    success: function(data) {
        // the next thing you want to do 
        var obj = data;
        console.log(obj);
        //for (var i = 0; i < data.length; i++) {
        //console.log("PAIR " + i + ": " + data[i].sysid);
        //console.log("PAIR " + i + ": " + data[i].name);
        //}


    }
});

1 个答案:

答案 0 :(得分:1)

$.ajax({
    type: 'GET',
    url: '../include/country.php',
    dataType : "json",
    data: {
        id: id
    },
    success: function(data) {
       for(var i = 0; i < data.length; i++) {
           console.log("PAIR " + i + ": " + data[i].sysid);
           console.log("PAIR " + i + ": " + data[i].name);
       }
    }
});