1
我有机器人的名字和他们的参数和配置组成的JSON文件,它看起来像这样:JSON文件到Ruby哈希
{
"csv-collector": {
"parameters": {
"delete_file": false,
"feed": "FileCollector",
"provider": "ola",
"path": "/tmp/",
"postfix": ".csv",
"rate_limit": 300
},
"group": "Collector",
"name": "Fileinput",
"module": "abc",
"description": "Fileinput collector fetches data from a file."
},
"csv-parser": {
"parameters": {
"columns": "classification.type,destination.ip",
"default_url_protocol": "http://",
"delimiter": ",",
"skip_header": true,
"type": "c&c"
},
"group": "Parser",
"name": "Generic CSV",
"module": "efg",
"description": "Generic CSV Parser is a generic bot"
}
}
而且我想它与机器人的名字解析到一个Ruby的Hash “csv-collector”和“csv-parser”作为键和其余内容作为值。 事情是这样的:
my_hash = {"csv-collector" => {"parameters": {
"delete_file": false,
"feed": "FileCollector",
"provider": "ola",
"path": "/tmp/",
"postfix": ".csv",
"rate_limit": 300
},
"group": "Collector",
"name": "Fileinput",
"module": "abc",
"description": "Fileinput collector fetches data from a file."
}
}
我有几个机器人因此必须为他人有效为好。
我已经有下面的代码:
require "json"
def read_file
temp = Hash.new
file = JSON.parse(File.read('mybotsfile.conf'))
file.each { |bot| temp << bot}
puts temp
end
不过是给我下面的错误:
`undefined method `<<' for {}:Hash (NoMethodErr`or)
我是新来的Ruby和我不很清楚如何解析JSON文件到红宝石哈希