Ruby Hash的JSON文件

mf3*_*370 1 ruby json file

我有一个由机器人名称及其参数和配置组成的JSON文件,它看起来像这样:

{
    "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"
    }
}
Run Code Online (Sandbox Code Playgroud)

我想将它解析为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."
            }
         }
Run Code Online (Sandbox Code Playgroud)

我有几个机器人,所以这也必须对其他机器人有效.

我已经有以下代码:

require "json"

def read_file
  temp = Hash.new
  file = JSON.parse(File.read('mybotsfile.conf'))
  file.each { |bot| temp << bot}
  puts temp
end
Run Code Online (Sandbox Code Playgroud)

但是给了我以下错误:

`undefined method `<<' for {}:Hash (NoMethodErr`or)
Run Code Online (Sandbox Code Playgroud)

我是Ruby的新手,我不太清楚如何将JSON文件解析为Ruby Hash

And*_*eko 6

require 'json'
file = File.read('your-json-file.json')
result_hash = JSON.parse(file)
Run Code Online (Sandbox Code Playgroud)