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 :)

5
  • 1
    What do you mean by "count the object"? Commented Oct 13, 2015 at 7:48
  • It is very dificult to answer without the 'structure' of json in the response Commented Oct 13, 2015 at 7:48
  • i added my response @Anonymous0day Commented Oct 13, 2015 at 7:53
  • It's still unclear what "count the object" means. An object is a single thing. So it's "1". Commented Oct 13, 2015 at 7:55
  • I assume you mean "count the properties", which is a duplicate. If not, please clarify your question. Commented Oct 13, 2015 at 8:03

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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 ?');  
  });

4 Comments

What's so important about the return? You are not even making use of it.
To chain without callback, i will update my answer to understand
That doesn't seem to have anything to do with the question though.
@FelixKling you right ! it is a plus ! :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.