I''m getting an XML-document form a third party URL, parse it using Hpricot and Xpath and wish to return the result as JSON. However, I can''t find a decent way to convert XML to JSON, is there a nice Railsy shortcut for this that I haven''t discovered? Thanks! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
There is in fact a wonderful little thing known as Object#to_json check it out: http://rails.rubyonrails.com/classes/Object.html#M000143 - Kyle --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Kyle Neath wrote:> There is in fact a wonderful little thing known as Object#to_json > check it out: http://rails.rubyonrails.com/classes/Object.html#M000143 > > - KyleThanks, but I''ve already tried it and it doesn''t work (get ActiveSupport::JSON::CircularReferenceError). My guess is that I need to convert the XML to a hash, which I''ve tried using Hash.from_xml(parsed_xml) but I get a NameError in the controller: "undefined local variable or method `source'' for REXML::SourceFactory:Class". Anyone know of another way besides Hash.from_xml to convert the XML to a hash before I convert it to JSON? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Just a guess, but you might want to investigate the options available for :to_json. For example, passing :only=>[attr_list] for an array will cause it to render only the named attributes for each member of the collection. It''s possible that by the right combination of options you can break the circular reference issue. On Jan 28, 6:40 pm, Gu stav <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Kyle Neath wrote: > > There is in fact a wonderful little thing known as Object#to_json > > check it out:http://rails.rubyonrails.com/classes/Object.html#M000143 > > > - Kyle > > Thanks, but I''ve already tried it and it doesn''t work (get > ActiveSupport::JSON::CircularReferenceError). > > My guess is that I need to convert the XML to a hash, which I''ve tried > using Hash.from_xml(parsed_xml) but I get a NameError in the controller: > "undefined local variable or method `source'' for > REXML::SourceFactory:Class". > > Anyone know of another way besides Hash.from_xml to convert the XML to a > hash before I convert it to JSON? > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---