Convert XML to this Ruby data structure

I am getting data from an api that returns xml like this:

   

I am new to deserialization, but I think it is appropriate to parse this xml into a ruby Object, then I can reference like object Foo.seriess.series.frequency returns’Quarterly’ like that.

From my search here and Google, there does not seem to be an obvious solution. This is in Ruby (NOT rails), which makes me think I am missing something fairly obvious. Any ideas?

Edit
I set up a test case based on Winfield’s suggestion.

class Exopenstruct

require'ostruct'

def initialize()

hash = {"seriess"=>{"realtime_start"=>"2013-02-01", "realtime_end"=>"2013- 02-01", "series"=>{"id"=>"GDPC1", "realtime_start"=>"2013-02-01", "realtime_end"=>"2013-02-01", "title"= >"Real Gross Domestic Product, 1 Decimal", "observation_start"=>"1947-01-01", "observation_end"=>"2012-10-01", "frequency"=>"Quarterly", "frequency_short"= >"Q", "units"=>"Billions of Chained 2005 Dollars", "units_short"=>"Bil. of Chn. 2005 $", "seasonal_adjustment"=>"Seasonally Adjusted Annual Rate", "seasonal_adjustment_short"=> "SAAR", "last_updated"=>"2013-01-30 07:46:54-06", "popularity"=>"93", "notes"=>"Real gross domestic product is the inflation adjusted value of the goods and services produced by labor and property located in the United States. For more information see the Guide to the National Income and Product Accounts of the United States (NIPA) -(http://www.bea.gov/national/pdf/nipaguid.pdf)"}}}

object_instance = OpenStruct.new( hash )

end< br />end

In irb I loaded the rb file and instantiated the class. However, when I try to access a property (such as instance.seriess), I receive: NoMethodError: undefined method `seriess’

If I missed something obvious, I apologize again.

You may better use standard XML for hash parsing, such as the one contained in Rails:

object_hash = Hash.from_xml(xml_string)
puts object_hash['seriess']

If you are not using the Rails stack, you can use a library like Nokogiri to achieve the same behavior.

Edit: If you are looking for object behavior, Using OpenStruct is a good way to wrap the hash:

object_instance = OpenStruct.new( Hash.from_xml(xml_string) )
puts object_instance.seriess

pre>

Note: For deeply nested data, you may also need to recursively convert the embedded hash value into an OpenStruct instance. IE: If the above attribute is a hash value of the value, then it will be Ha Hope value instead of OpenStruct.

I am getting data from api that returns xml like this:

   

I am new to deserialization, but I think it is appropriate to add this The xml is parsed into a ruby ​​object, and then I can refer to return'Quarterly' like objectFoo.seriess.series.frequency.

From my search here and Google, there does not seem to be an obvious solution, This is in Ruby (NOT rails), which makes me think I am missing something quite obvious. Any ideas?

Edit
I set up a test case based on Winfield's suggestion.

class Exopenstruct

require'ostruct'

def initialize()

hash = {"seriess"=>{"realtime_start"=>"2013-02-01", "realtime_end"=>"2013- 02-01", "series"=>{"id"=>"GDPC1", "realtime_start"=>"2013-02-01", "realtime_end"=>"2013-02-01", "title"= >"Real Gross Domestic Product, 1 Decimal", "observation_start"=>"1947-01-01", "observation_end"=>"2012-10-01", "frequency"=>"Quarterly", "frequency_short"= >"Q", "units"=>"Billions of Chained 2005 Dollars", "units_short"=>"Bil. of Chn. 2005 $", "seasonal_adjustment"=>"Seasonally Adjusted Annual Rate", "seasonal_adjustment_short"=> "SAAR", "last_updated"=>"2013-01-30 07:46:54-06", "popularity"=>"93", "notes"=>"Real gross domestic product is the inflation adjusted value of the goods and services produced by labor and property located in the United States. For more information see the Guide to the National Income and Product Accounts of the United States (NIPA)-( http://www.bea.gov/national/pdf/nipaguid.pdf)"}}}

object_instance = OpenStruct.new( hash )

end
end

In irb I loaded the rb file and instantiated the class. However, when I try to access a property (such as instance.seriess), I receive: NoMethodError: undefined method`seriess '

If I missed something obvious, I apologize again.

You might better use standard XML for hash parsing, For example, Rails contains:

object_hash = Hash.from_xml(xml_string)
puts object_hash['seriess']

If You don't use a Rails stack, you can use a library like Nokogiri to achieve the same behavior.

Edit: If you are looking for object behavior, using OpenStruct is a good way to wrap the hash:< /p>

object_instance = OpenStruct.new( Hash.from_xml(xml_string) )
puts object_instance.seriess

Note: For deeply nested data, You may also need to recursively convert the embedded hash value into an OpenStruct instance. IE: If the above attribute is a hash value of the value, it will be the hash value instead of OpenStruct.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 6057 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.