Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create json object from string with ruby [closed]

I am really new about Ruby development. I am trying to create a json file with strings. My json file like below. Can you help to me

{
    "App":{
        "properties":{"color":"red"},
        "screens":[
                {"id":"page1", "properties":{"color":"red"}, "elements":[
                                                {"type":"txtbox", "properties":{"color":"red"}}, 
                                                {"type":"button", "properties":{"color":"red"}}
                                                ]
                },
                {"id":"page2", "properties":{"color":"red"}, "elements":[
                                                {"type":"txtbox", "properties":{"color":"red"}}, 
                                                {"type":"button", "properties":{"color":"red"}}
                                                ]
                }
        ]
    }
}
like image 756
hiwordls Avatar asked Apr 21 '15 08:04

hiwordls


1 Answers

You can parse JSON with ruby from a hash:

require 'json'

my_hash = JSON.parse('{"hello": "goodbye"}')
puts my_hash["hello"] => "goodbye"

Or generate it from a hash:

require 'json'

my_hash = {:hello => "goodbye"}
puts JSON.generate(my_hash) => "{\"hello\":\"goodbye\"}"

Have a look at the JSON documentation.

like image 99
Will Clarke Avatar answered Sep 21 '22 22:09

Will Clarke