0

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!

3
  • this one needs a jsfiddle Jan 27, 2015 at 19:12
  • What is the output of console.log(e)? Your logic looks fine.
    – joews
    Jan 27, 2015 at 19:14
  • {path=me/friends, success=true, result={"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=XXXXXXXXXXXX"},"summary":{"total_count":8}}, code=0} Jan 27, 2015 at 21:38

1 Answer 1

5

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.