Starx
Starx

Reputation: 79059

Accessing JSON encoded MuliDimensional array using jQuery

I have an multidimensional array

$array = array(
    "Level1"=>array(
        "Level11"=>array(
            "level111"=>'value1',
            "level112"=>'value2',
            ),
        "Level12"=>array(
            "level121"=>'value1',
            "level122"=>'value2',
            ),
        ),
    "Level2"=>array(
        "Level21"=>array(
            "level211"=>'value1',
            "level212"=>'value2',
            ),
        "Level22"=>array(
            "level221"=>'value1',
            "level222"=>'value2',
            ),
        )
    );
echo json_encode($array);

This encoded JSON is sent after receiving AJAX POST request using jQuery.

$.post(
    'mypage.php',
    {
        param1: value1,
        param2: value2
    },
    function(data) {
        //Now I can access the 1st level JSON value easily like
        alert(data.Level1); 

        // But

        // I am trying to access the values like 

        alert(data.Level1.Level11.level112); //which is not possible
    },
    "json"
);

If you have understood my question, do you know how I could tackle this problem.

Upvotes: 0

Views: 453

Answers (1)

Felix Kling
Felix Kling

Reputation: 817198

Ok, my guess: You use capital letters in some of your keys in PHP but not in JS. Your line should be:

data.Level1.Level11.level112

Note that it is Level1 with captial L, not level1.

DEMO

Upvotes: 1

Related Questions