Jefferson X Masonic
Jefferson X Masonic

Reputation: 593

How do I get a list of users from the Json API?

I want to download the AJAX user list available at http://zadanie.ee/users.json, And I would like to display

1.The number of all users, 2.The Number of active users, 3.The Number of active women, 4.The Number of active men,

from JSON API, using jquery/javascript etc,

How can I achieve this? or can some one provide me a simple tutorial to start with?

Here is what I have done so far :

CODE:

<script>

function ViewData()
{

  $.getJSON("http://zadanie.ee/users.json",
   function(data)
   {
     var items = [];

     $.each(data.Users,
      function(key, value)
      {
        items.push("<li>" +
         value.username + "<br />" +
         value.active + "<br />" +

         + "</li>");
      });
     $('<ul/>', {html: items.join(")}).
      appendTo('body');
   });
}
</script>

Any help or any idea will be appreciated, Thanks.

Upvotes: 0

Views: 1332

Answers (2)

Please try this code.

$(document).ready(function (){
         $.getJSON("http://zadanie.laboratorium.ee/users.json",function(data)
        {
             var items = [];
             var number_of_user = 0;
             var number_of_active_user = 0;
             var number_of_active_men = 0;
             var number_of_active_women = 0;
                 $.each(data,
                  function(key, value)
                  {
                    number_of_user += 1;
                    if(value['active'] == true){
                        if(value['gender'][0] == 'Female' || value['gender'][0] == 'female'){
                            number_of_active_women +=1;
                        }
                        else if(value['gender'][0] == 'Male' || value['gender'][0] == 'male'){
                            number_of_active_men +=1;
                        }
                    }

                  });
                console.log("number_of_user ==> "+number_of_user +" number_of_active_user ==> "+ number_of_active_user +" number_of_active_men ==> "+ number_of_active_men +" number_of_active_women ==> "+ number_of_active_women);
       });
     }); 

Hope this helps.

Upvotes: 2

Danyal Imran
Danyal Imran

Reputation: 2605

Use AJAX to read data and then parse it accordingly.

$.ajax({ 
    type: 'GET', 
    url: '"http://zadanie.laboratorium.ee/users.json"', 
    dataType: 'json',
    success: function (data) { 
        $.each(data, function(index, element) {
            console.log(element.username);
        });
    }
});

Upvotes: 0

Related Questions