简体   繁体   中英

How do you consume a json or xml web service in Rails or Ruby?

An external web service is sending json or xml towards my app. What would be the best way to consume this data? Is there a built-in library in Rails or Ruby to do this or do I need to use a gem for this?

For parsing json, you can use rails' method ActiveSupport::JSON.decode directly in your controller code. For what concerns XML parsing instead, you can choose between rexml (included in the ruby Standard Library), hapricot and libxml-ruby (available via ruby gem).

The choice is ultimately a matter of taste and performance. This benchmark claims that libxml-ruby is the fastest one, though it requires a few other libraries.

if it is a web service you query as a source of data, consider making an ActiveResource here is a railscast of the basics

http://railscasts.com/episodes/94-activeresource-basics

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM