2017-02-10 6 views
1

私はボットの名前とそのパラメータと設定で構成されるJSONファイルを持っており、それは次のようになります。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" 
    } 
} 

そして、私はボット名でRubyのハッシュにそれを解析したいと思います"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." 
      } 
     } 

は、私はいくつかのボットを持っているので、これは、同様に他の人のために有効である必要があります。

私はすでに次のコードを持っている:私はルビーに新たなんだと私は解析する方法を非常によく知っていない

`undefined method `<<' for {}:Hash (NoMethodErr`or) 

require "json" 

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

しかし、私は次のエラーを与えているがRubyハッシュへのJSONファイル

答えて

5
require 'json' 
file = File.read('your-json-file.json') 
result_hash = JSON.parse(file)