json encode multidimensional array get value

懵懂的女人 提交于 2019-12-25 01:48:54

问题


 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
        )
)

This is my array that i get from my print_r request it is from php it is a multidimensional array that i used json_encode now this is the data i get from success. I want to get all the value of sysid and name from this json to be put in select option how is this possible

I used this code below but i get undefined and i get 4,000 result

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

my bad the first one is the print_r this is the 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 is

$.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:


$.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);
       }
    }
});


来源:https://stackoverflow.com/questions/27995952/json-encode-multidimensional-array-get-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!