Running json using jquery Ajax

0

I have a question, how can I go through Json and retrieve specific objects through ajax?

Code :

function loadJson(){

$.ajax({
  url: "data.json",
  context: document.body
}).done(function() {
  $( this ).addClass( "done" );
  $(".done").load("data.json");
});

}

Json :

$.json
[  
   {  
      "name":"Rio de janeiro",
      "contents":[  
         {  
            "moment":"Rio de janeiro",
            "gallery":null,
            "tags":[  
               "Calor",
               "Verão",
               "Sol"
            ],
            "text":"ENxuKBLPgIuaevUGsxahYQwggHiheIBHsYxwiAborXeqJRoacSbdkYeufLSdnSiRzUnuVRuKktmBTDltuJXryfKyxXjDrACJXgIU",
            "contentUrl":null,
            "createdAt":1456231430795,
            "type":"TEXT",
            "socialNetwork":"TWITTER",
            "userName":"MatSproesser",
            "avatarUrl":"https://pbs.twimg.com/profile_images/1207792762/opf_normal.jpg"
         },
    
asked by anonymous 24.02.2016 / 13:15

1 answer

0

When you load a valid JSON via $ .ajax the content of your json will be transformed into a javascript object, then all properties of your JSON will be accessible according to the name of each property in the code below I'll show you how to access the name and tags:

$(document).ready(function () {

  $.ajax('data.json').then(function(data) {
    console.log(data);
    console.log('name', data.name);
    console.log('tags', data.contents[0].tags);
  });

});
    
24.02.2016 / 14:06