我有一个我想在Ruby中解析的JSON。 Ruby对我来说是全新的,但我必须使用它: - )
这是我的litte片段,应该进行解析:
response = File.read("app/helpers/example_announcement.json")
JSON.parse(response)
这很好用。唯一的缺点是,我不知道我使用它的属性,它不是类型安全的。所以我为它创建了对象
class Announcements
@@announcements = Hash # a map key => value where key is string and value is type of Announcement
end
class Announcement
@@name = ""
@@status = ""
@@rewards = Array
end
这就是json的样子
{
"announcements": {
"id1" : {
"name": "The Diamond Announcement",
"status": "published",
"reward": [
{
"id": "hardCurrency",
"amount": 100
}
]
},
"id2": {
"name": "The Normal Announcement",
"players": [],
"status": "published",
"reward": []
}
}
}
所以我尝试了像这样的JSON解析
response = File.read("app/helpers/example_announcement.json")
JSON.parse(response, Announcements)
但这不是它的工作方式^^任何人都可以帮我这个吗?