Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

json

jquery

People also ask

How do I count the number of records in JSON?

“how to count number of records in json” Code AnswerCall len(obj) to return the number of items in a JSON object obj.

How do you find the number of elements in a JSON array?

Description. JsonArray::size() gets the number of elements in the array pointed by the JsonArray . If the JsonArray is null, this function returns 0 .

How many JSON objects are represented?

JSON Syntax JSON defines only two data structures: objects and arrays. An object is a set of name-value pairs, and an array is a list of values. JSON defines seven value types: string, number, object, array, true, false, and null.

How many rows can JSON handle?

The default is 2097152 characters, which is equivalent to 4 MB of Unicode string data. JSON itself has no inherent limit.


If you have something like this:

var json = [ {a:b, c:d}, {e:f, g:h, ...}, {..}, ... ]

then, you can do:

alert(json.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.


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.


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.