2015-04-21 11 views
6

Tôi thực sự mới về phát triển Ruby. Tôi đang cố gắng để tạo ra một tập tin json với chuỗi. Tập tin json của tôi như dưới đây. bạn có thể giúp tôitạo đối tượng json từ chuỗi với ruby ​​

{ 
    "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"}} 
               ] 
       } 
     ] 
    } 
} 

Trả lời

15

Bạn có thể phân tích cú pháp JSON với ruby ​​từ một hash:

require 'json' 

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

Hoặc tạo nó từ một băm:

require 'json' 

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

Có một cái nhìn tại JSON documentation.

Các vấn đề liên quan