日期:2014-05-16  浏览次数:20276 次

xml,json转换为hash
ruby xml json转换为hash

ActiveSupport:

   Xml to Hash => Hash.from_xml(xmlstr)

   Json to Hash => ActiveSupport::JSON.decode(jsonstr)

JnuemakerCrack:

   Xml to Hash => Crack::XML.parse(xmlstr)

   Json to Hash => Crack::JSON.parse(jsonstr)

ps:若想将hash转为xml或json

ActiveSupport:

   Hash to xml  => Hash.to_xml

   Hash to json => Hash.to_json 或 ActiveSupport::JSON.encode(hash)

其它省略,可自行查找相关gem或自行实现转换。

从Benchmark结果上来看,ruby中xml,json转为Hash 使用Crack 比 ActiveSupport要快。因而推荐使用--crack。