Agata Agata - 1 year ago 55
Javascript Question

JsonObject dimension doesn't work

I have a simple problem with jsonObject dimension.
I need to create a dynamic table that will add number of row equals to the record that are in the jsonObject.
My code is as follow:

var jsonObject = eval('(' + xmlhttp.responseText + ')');

var count=Object.keys(jsonObject).length;
alert('count2 equal'+count);


When I execute it, count values is 2.
This is the content of jsonObject in the servlet:

{
"type" : "perso_atti",
"values" : [{
"CF" : "hygt45rfd56tgyui",
"costo_orario" : 20,
"cup" : "1",
"data" : "2015-01-07",
"id_Attivita" : 1,
"numeroOre" : 4
}, {
"CF" : "hygt45rfd56tgyui",
"costo_orario" : 20,
"cup" : "1",
"data" : "2015-01-12",
"id_Attivita" : 1,
"numeroOre" : 4
}
....


I don't understand why the value is 2.
I have 30 record so it may be 30.

Answer Source

var count = Object.keys(jsonObject).length is 2 because there are only 2 keys: type and values. I believe what you want is

var count = Object.keys(jsonObject.values).length;

Or you can just do var count = jsonObject.values.length as @FelixKling suggests.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download