SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: JSON length

  1. #1
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question JSON length

    Hey guys,

    I am getting the following response from Ajax:

    Code:
    [{"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:
    PHP Code:
    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!

    Thank you

  2. #2
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe put your assignment inside the eval:
    Code:
    var json;
    eval('(json = ' + request.responseText + ')');

  3. #3
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nope... same thing... thanks though

    Quote Originally Posted by r51 View Post
    Maybe put your assignment inside the eval:
    Code:
    var json;
    eval('(json = ' + request.responseText + ')');

  4. #4
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rosy View Post
    Hey guys,

    I am getting the following response from Ajax:

    Code:
    [{"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:
    PHP Code:
    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!

    Thank you
    Hi.
    You are managing an array with an object
    inside.
    An object doesn't support the length property
    look at your code
    PHP Code:
    [{}] 
    Try this
    PHP Code:
    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...uide/Variables

    Bye.
    Last edited by whisher; Aug 23, 2008 at 01:28.

  5. #5
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about
    Code:
    json[0]["Account Name"].length;


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •