Parsing an json array

Discussion in 'JavaScript' started by rob_v, Feb 13, 2008.

  1. #1
    Ok - im sure this is pretty basic - but I cant wrap my head around it.
    (Im new to js)

    Im hitting an outside php script and returning a json string to javascript.
    (also - what is the proper term - is it a json string or json array?)
    But basically it looks like this when JS gets it from php :

    {"account_id":"123","account_type_id":"10","account_role_id":"40","account_username":"test123","account_password":"passwd","account_email":"test@test.com"}

    Once I get it back into javascript - I want to just loop thru it and echo out each key and value.
    (I dont know what they key name are - they can change each time)
    In php id do something like this :
    foreach ($array as $key=>$value)
    {
    echo "$key ---> $value";
    }

    How do I do the same thing for a json string?

    Thanks
     
    rob_v, Feb 13, 2008 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    for (var i in json_array)
    {
    	alert(i); // Key
    	alert(json_array[i]); // Value
    }
    
    
    Code (javascript):
     
    nico_swd, Feb 13, 2008 IP
  3. rob_v

    rob_v Peon

    Messages:
    72
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    perfect - thanks
    I knew it was easy ;)
     
    rob_v, Feb 13, 2008 IP
  4. The Critic

    The Critic Peon

    Messages:
    392
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    It's actually an object containing name:value pairs in the form of strings.
     
    The Critic, Feb 15, 2008 IP