Is there a library to convert XML to JSON in Ruby?
相关问题
- Jackson Deserialization not calling deserialize on
- Illegal to have multiple roots (start tag in epilo
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Easiest way to get json and parse it using JQuery
相关文章
- json_encode 没有把数组转为json
- Livy Server: return a dataframe as JSON?
- Creating XML Elements without namespace declaratio
- Ruby using wrong version of openssl
- Get Attribute Value From Simple XML Using JQuery /
- Unexpected end of JSON input from an ajax call
- Difference between Thread#run and Thread#wakeup?
- how to call a active record named scope with a str
A simple trick:
First you need to
gem install json
, then when using Rails you can do:If you are not using Rails, then you can
gem install activesupport
, require it and things should work smoothly.Example:
If you wish to keep all attributes I recommend cobravsmongoose http://cobravsmongoose.rubyforge.org/ which uses the badgerfish convention.
becomes:
code:
You may find the
xml-to-json
gem useful. It maintains attributes, processing instruction and DTD statements.Install
Usage
Produces:
It's a simple derivative of
xml-to-hash
.If you're looking for speed I would recommend Ox since it's pretty much the fastest option from the ones already mentioned.
I ran some benchmarks using an XML file that has 1.1 MB from omg.org/spec and these are the results(in seconds):
I'd use Crack, a simple XML and JSON parser.
Assuming you're using libxml, you can try a variation of this (disclaimer, this works for my limited use case, it may need tweaking to be fully generic)