Booster4App
Booster4App

Reputation: 57

Javascript (Appcelerator Titanium) Multidimensional Array parse doesn't work

I have an problem with the parsing of an multidimensional array, i get this from facebook as an answer:

In e.result i have this:

{
   "data":[
        {
         "name":"Linda Kase",
         "id":"1393034660999695"
        },
        {
         "name":"Dick Typ",
         "id":"1376046576034204"
        },
        {
         "name":"a a",
         "id":"1388801108093951"
        },
        {
         "name":"b b",
         "id":"1382328532076389"
        }
     ],
   "paging": {
        "next":"https:\/\/graph.facebook.com\/v2.2\/1378163649155341\/friends?format=json&access_token=XXXXXXXXXXXXXXXXXXX"
     },
   "summary":{
            "total_count":8
         }
}

and this is my code to parse it:

Ti.App.fb.requestWithGraphPath('me/friends',false, 'GET', function(e){
    if(e.success){
        var result = e.result;
        alert(result.data[0].name);
    }
});

I always recieve this:

Uncaught TypeError: Cannot read property '0' of undefined

Anyone an idea why it isn't working ? I've also tried to JSON.stringify and JSON.parse the e.result

Thank you!

Upvotes: 0

Views: 215

Answers (1)

FirePanther
FirePanther

Reputation: 355

You have to use:

    var result = JSON.parse(e.result);
    alert(result.data[0].name);

because you get it as a string, not as an array

Upvotes: 5

Related Questions