How to convert a ruby hash object to JSON?

前端 未结 5 649
清酒与你
清酒与你 2020-11-29 15:14

How to convert a ruby hash object to JSON? So I am trying this example below & it doesn\'t work?

I was looking at the RubyDoc and obviously Hash obj

5条回答
  •  死守一世寂寞
    2020-11-29 15:55

    Add the following line on the top of your file

    require 'json'
    

    Then you can use:

    car = {:make => "bmw", :year => "2003"}
    car.to_json
    

    Alternatively, you can use:

    JSON.generate({:make => "bmw", :year => "2003"})
    

提交回复
热议问题