Ruby objects and JSON serialization (without Rails)

前端 未结 11 1963
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-28 03:32

I\'m trying to understand the JSON serialization landscape in Ruby. I\'m new to Ruby.

Is there any good JSON serialization options if you are not working with Rails?

11条回答
  •  盖世英雄少女心
    2020-11-28 04:25

    What version of Ruby are you using? ruby -v will tell you.

    If it's 1.9.2, JSON is included in the standard library.

    If you're on 1.8.something then do gem install json and it'll install. Then, in your code do:

    require 'rubygems'
    require 'json'
    

    Then append to_json to an object and you're good to go:

    asdf = {'a' => 'b'} #=> {"a"=>"b"}
    asdf.to_json #=> "{"a":"b"}"
    

提交回复
热议问题