Search code examples
javascriptjqueryjsonbackbone.js

How to get the data in Json Array? -Backbone js


Im currently working on Backbone js that reads json.. I get the json in the api using JSON.stringify and it displays as JSON but when i get the display_name it gets undefined

This is my JSON Result(this json is from my dropbox API)

 [  
   {  
      "referral_link":"https://db.tt/JnPooKxuE1",
      "display_name":"Sample APP",
      "uid":638229321,
      "locale":"en",
      "team":null,
      "quota_info":{  
         "datastores":0,
         "shared":0,
         "quota":2147483648,
         "normal":9972222
      },
      "is_paired":false,
      "country":"EU",
      "name_details":{  
         "familiar_name":"Sample",
         "surname":"App",
         "given_name":"Sample"
      },
      "email":"[email protected]"
   }
]

This is the code when i get my json using backbone js

$(function(){


              var access_token1 = 'my token';

             var Profile = Backbone.Model.extend();

             var ProfileList = Backbone.Collection.extend({
               model: Profile,
               url: 'https://api.dropboxapi.com/1/account/info'
             });

                var setHeader = function (xhr) {
                     xhr.setRequestHeader('Content-Type', 'application/json');
                       xhr.setRequestHeader("Authorization", 'Bearer ' + access_token1);
                    }



             var profiles = new ProfileList();

             /*var jsontree = JSON.stringify(profiles);*/


             profiles.fetch({ beforeSend: setHeader });
            profiles.bind('reset', function () {


                        var result = JSON.stringify(profiles.models);


                /*        document.getElementById("demo").innerHTML = result[0];*/
                          console.log(result);




              });
      });

Solution

  • Since you have an object inside an array you'll need to select the first item in the array before selecting the object key.

    result[0]["display_name"]