JSON length

Hey guys,

I am getting the following response from Ajax:


[{"Account Name":[{"id":"944ef382-becc-102b-baf8-000c29f242f5","priority":"1","creator_id":"Chris Kelly","object_id":"32d79cfd-b6f6-102b-a73c-000c29bdb9c5","created_at":"August 18, 2008 @ 04:21 PM","date":"18 AUGUST","note":"I will note too!!"},{"id":"753d3736-becc-102b-baf8-000c29f242f5","priority":"0","creator_id":"Moon","object_id":"32d79cfd-b6f6-102b-a73c-000c29bdb9c5","created_at":"August 18, 2008 @ 04:20 PM","date":"18 AUGUST","note":"One more note of the day!"},{"id":"906d07a4-bc72-102b-ad39-000c29bdb9c5","priority":"0","creator_id":"Moon","object_id":"32d79cfd-b6f6-102b-a73c-000c29bdb9c5","created_at":"August 15, 2008 @ 04:32 PM","date":"15 AUGUST","note":"Moon is good at this"},{"id":"7c7b2c6d-bc72-102b-ad39-000c29bdb9c5","priority":"0","creator_id":"Moon","object_id":"32d79cfd-b6f6-102b-a73c-000c29bdb9c5","created_at":"August 15, 2008 @ 04:31 PM","date":"15 AUGUST","note":"This is a normal notes"},{"id":"76eb582c-bc72-102b-ad39-000c29bdb9c5","priority":"1","creator_id":"Moon","object_id":"32d79cfd-b6f6-102b-a73c-000c29bdb9c5","created_at":"August 15, 2008 @ 04:31 PM","date":"15 AUGUST","note":"This is high!"},{"id":"6c06f918-bc72-102b-ad39-000c29bdb9c5","priority":"2","creator_id":"Moon","object_id":"32d79cfd-b6f6-102b-a73c-000c29bdb9c5","created_at":"August 15, 2008 @ 04:31 PM","date":"15 AUGUST","note":"This account sucks!"}]}]

Here is the Javascript:


var json = eval('(' + request.responseText + ')');

alert(json.length); // Output is 1
alert(json[0].length); // Output is undefined

So, how can I get the count of all array elements inside “Account Name”?

Any possible help / tips will be highly appreciated! :slight_smile:

Thank you

Maybe put your assignment inside the eval:


var json;
eval('(json = ' + request.responseText + ')');

Nope… same thing… thanks though :slight_smile:

Hi.
You are managing an array with an object
inside.
An object doesn’t support the length property
look at your code


[{}]

Try this


alert(json[0].accountName.length);

you must change the name
of account name
You can’t declare a var with a
blank space inside.
http://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Variables

Bye.

How about


json[0]["Account Name"].length;