Hello, I am using Ajax.Request to retrieve a JSON object from my server. The result looks something this: {"customers":{"342":"Mark","432":"John","522":"Mary","121":"Tom"}} I parse the returned JSON data with " var mydata transport.responseText.evalJSON(true); " to get an object. So how can I can now loop though the object to access each key / value pair and do something with it? I am assuming that I don''t know the keys, so I need to read out the keys as well. I tried using .each without any success... Any ideas? greg --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Gregory Stewart wrote:> I am using Ajax.Request to retrieve a JSON object from my server. > > The result looks something this: > > {"customers":{"342":"Mark","432":"John","522":"Mary","121":"Tom"}} > > I parse the returned JSON data with " var mydata > transport.responseText.evalJSON(true); " to get an object. > > So how can I can now loop though the object to access each key / value > pair and do something with it? > I am assuming that I don''t know the keys, so I need to read out the > keys as well.JSON is just a normal Javascript object. So you''d just use a simple for/in loop.> I tried using .each without any success....each is a method of an Enumberable object. If you want to use it create a Hash object first out of your JSON. $H(mydata).each(...) -- Michael Peters Developer Plus Three, LP --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks a lot! Makes sense greg On May 17, 1:33 pm, Michael Peters <mpet...-aUYv5hkjw45l57MIdRCFDg@public.gmane.org> wrote:> Gregory Stewart wrote: > > I am using Ajax.Request to retrieve a JSON object from my server. > > > The result looks something this: > > > {"customers":{"342":"Mark","432":"John","522":"Mary","121":"Tom"}} > > > I parse the returned JSON data with " var mydata > > transport.responseText.evalJSON(true); " to get an object. > > > So how can I can now loop though the object to access each key / value > > pair and do something with it? > > I am assuming that I don''t know the keys, so I need to read out the > > keys as well. > > JSON is just a normal Javascript object. So you''d just use a simple for/in loop. > > > I tried using .each without any success... > > .each is a method of an Enumberable object. If you want to use it create a Hash > object first out of your JSON. > > $H(mydata).each(...) > > -- > Michael Peters > Developer > Plus Three, LP--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---