[jquery] How do I get the total Json record count using JQuery?

I couldn't find anything about getting the total JSON record count using jQuery.

Here is the JSON returned:

{"Email":"Please enter your Email.","Password":"Please enter a password."}

Here is my code:

$(function() {
    $("#btnSubmit").click(function() {
        $.ajax({
            url: "/account/signup",
            type: "POST",
            dataType: "json",
            data: { 
                Email: $("#strEmail").val(),
                Password: $("#strPassword").val()
            },
            success: function(j) {
                    $(".errMsg").hide();
                    alert(j.length); // I couldn't get the total count
                $.each(j, function(n) {
                    $("#err" + n).html(j[n]);
                    $("#err" + n).show();
                })

            },
            error: function(req, status, error) {
                alert(req);
            }
        });
    });
});

This question is related to jquery json

The answer is


Why would you want length in this case?

If you do want to check for length, have the server return a JSON array with key-value pairs like this:

[
  {key:value},
  {key:value}
]

In JSON, [ and ] represents an array (with a length property), { and } represents a object (without a length property). You can iterate through the members of a object, but you will get functions as well, making a length check of the numbers of members useless except for iterating over them.


Try the following:

var count=Object.keys(result).length;

Does not work IE8 and lower.


The OP is trying to count the number of properties in a JSON object. This could be done with an incremented temp variable in the iterator, but he seems to want to know the count before the iteration begins. A simple function that meets the need is provided at the bottom of this page.

Here's a cut and paste of the code, which worked for me:

function countProperties(obj) {
  var prop;
  var propCount = 0;

  for (prop in obj) {
    propCount++;
  }
  return propCount;
}

This should work well for a JSON object. For other objects, which may derive properties from their prototype chain, you would need to add a hasOwnProperty() test.


Perhaps you might want to try JSON.parse your return result first then you can get the count by .length


Why would you want length in this case?

If you do want to check for length, have the server return a JSON array with key-value pairs like this:

[
  {key:value},
  {key:value}
]

In JSON, [ and ] represents an array (with a length property), { and } represents a object (without a length property). You can iterate through the members of a object, but you will get functions as well, making a length check of the numbers of members useless except for iterating over them.


Why would you want length in this case?

If you do want to check for length, have the server return a JSON array with key-value pairs like this:

[
  {key:value},
  {key:value}
]

In JSON, [ and ] represents an array (with a length property), { and } represents a object (without a length property). You can iterate through the members of a object, but you will get functions as well, making a length check of the numbers of members useless except for iterating over them.


Perhaps you might want to try JSON.parse your return result first then you can get the count by .length


The OP is trying to count the number of properties in a JSON object. This could be done with an incremented temp variable in the iterator, but he seems to want to know the count before the iteration begins. A simple function that meets the need is provided at the bottom of this page.

Here's a cut and paste of the code, which worked for me:

function countProperties(obj) {
  var prop;
  var propCount = 0;

  for (prop in obj) {
    propCount++;
  }
  return propCount;
}

This should work well for a JSON object. For other objects, which may derive properties from their prototype chain, you would need to add a hasOwnProperty() test.


Your json isn't an array, it hasn't length property. You must change your data return or the way you get your data count.


Try the following:

var count=Object.keys(result).length;

Does not work IE8 and lower.


Your json isn't an array, it hasn't length property. You must change your data return or the way you get your data count.


What you're looking for is

j.d.length

The d is the key. At least it is in my case, I'm using a .NET webservice.

 $.ajax({
            type: "POST",
            url: "CantTellU.asmx",
            data: "{'userID' : " + parseInt($.query.get('ID')) + " }",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg, status) {
                ApplyTemplate(msg);
                alert(msg.d.length);
            }
        });

Your json isn't an array, it hasn't length property. You must change your data return or the way you get your data count.


Why would you want length in this case?

If you do want to check for length, have the server return a JSON array with key-value pairs like this:

[
  {key:value},
  {key:value}
]

In JSON, [ and ] represents an array (with a length property), { and } represents a object (without a length property). You can iterate through the members of a object, but you will get functions as well, making a length check of the numbers of members useless except for iterating over them.


What you're looking for is

j.d.length

The d is the key. At least it is in my case, I'm using a .NET webservice.

 $.ajax({
            type: "POST",
            url: "CantTellU.asmx",
            data: "{'userID' : " + parseInt($.query.get('ID')) + " }",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg, status) {
                ApplyTemplate(msg);
                alert(msg.d.length);
            }
        });

Your json isn't an array, it hasn't length property. You must change your data return or the way you get your data count.