4

How can i count the object of my request?

I am using ajax and requesting json data to this url pbxApi+"/conference/participants/"+circle+"/"+data.conference+"/"+data.uniqueid+'?jsonp=response'; and i want to count the object of the response .

This is my code

 var uri = pbxApi+"/conference/participants/"+circle+"/"+data.conference+"/"+data.uniqueid+'?jsonp=response';
        getJsonData(uri, function(res){
            console.log(res.length);
});

This is my functiion:

  var getJsonData = function(uri,callback){
    $.ajax({
      type: "GET",
      dataType: "jsonp",
      url: uri,
      jsonpCallback: 'response',
      cache: false,
      contentType: "application/json",
      success: function(json){
        callback(json);
      }
    });
  }

This is my response

response({"_id":"561713a78693609968e3bbdd","event":"ConfbridgeJoin","channel":"SIP/192.168.236.15-00000024","uniqueid":"1444352918.94","conference":"0090000293","calleridnum":"0090000288","calleridname":"0090000288","__v":0,"status":false,"sipSetting":{"accountcode":"0302130000","accountcode_naisen":"201","extentype":0,"extenrealname":"UID1","name":"0090000288","secret":"Myojyo42_f","username":"0090000288","context":"innercall_xdigit","gid":101,"cid":"0090000018"}})

Thanks :)

uno
  • 789
  • 1
  • 9
  • 26

3 Answers3

5

You can try this:

Object.keys(jsonArray).length;

to get the number of items in your JSON object.

Also refer Object.keys

Object.keys() returns an array whose elements are strings corresponding to the enumerable properties found directly upon object. The ordering of the properties is the same as that given by looping over the properties of the object manually.

Rahul Tripathi
  • 152,732
  • 28
  • 233
  • 299
0

You can

success: function(json){
    console.log('Object keys length: ' + Object.keys(json).length)
    callback(json);
}

For example {a:1, b:2, c:'Batman'} gives 3 as an answer

divoom12
  • 792
  • 3
  • 12
0

One Solution

 var uri = pbxApi+"/conference/participants/"+circle+"/"+data.conference+"/"+data.uniqueid+'?jsonp=response';

var getJsonData = function(uri,callback){
    return $.ajax({ // <----- note the return !
      type: "GET",
      dataType: "jsonp",
      url: uri,
      jsonpCallback: 'response',
      cache: false,
      contentType: "application/json",
      success: function(json){
        if(callback) callback(json);
      }
    });
  }

getJsonData(uri, function(res){
  console.log( Objec.keys(res).length) );
});

// with the return you will be able to do :
getJsonData(uri)
  .done( function(res){
    console.log( Objec.keys(res).length) );
  })
  .error(function( err ){
     console.log('an error ?? what is it possible ?');  
  });
Anonymous0day
  • 2,662
  • 1
  • 11
  • 15