【问题标题】:Read nested hash and generate separate hash of similar structure读取嵌套散列并生成类似结构的单独散列
【发布时间】:2016-08-23 16:34:26
【问题描述】:

我正在尝试读取 JSON 架构、解析特定内容并生成具有相同嵌套结构的新哈希。

我能够遍历原始架构,但是当我尝试创建新的嵌套哈希时,它被展平了。

这是我目前所拥有的:

require 'json'
@schema = JSON.parse("{
  "fields": [
    {
      "group": "common",
      "key": "1st_level"
    },
    {
      "object": "nested",
      "key": "order",
      "fields": [
        {
          "group": "common",
          "key": "2nd_level_1"
        },
        {
          "object": "nested",
          "key": "order2",
          "fields": [
            {
              "group": "common",
              "key": "3rd_level_1"
            }
          ]
        }
      ]
    }
  ]
}
")
@event_output = Hash.new

def parse_config(fields)
    if fields["group"].downcase == "common"
      @response = "HIT HERE"
    else
      @response = "INVALID GROUP"
    end
    return @response
end

def loop_params(parent, key, schema)
  @new_output = Hash.new
  @parent = parent
  schema["fields"].each do |fields|
    if fields["object"].nil? & !fields["group"].nil?
      @key = fields["key"]
      @repsonse = parse_config(fields)
      @new_output[@key] = @repsonse
      if key != ""
        @parent[key] = @new_output
      else
        @parent = @new_output
      end
    else
      print @parent
      @key = fields["key"]
      loop_params(@parent, @key, fields)
    end
  end
  return @parent
end
@event_output = loop_params(@event_output, "", @schema)
print "\n FINAL OUTPUT \n"
print @event_output

由此产生的输出是:

{
    "1st_level"=>"HIT HERE",
    "order"=>{
        "2nd_level_1"=>"HIT HERE"
    },
    "order2"=>{
        "3rd_level_1"=>"HIT HERE"
    }
}

很接近但我想要:

{
    "1st_level"=>"HIT HERE",
    "order"=>{
        "2nd_level_1"=>"HIT HERE",
        "order2"=>{
            "3rd_level_1"=>"HIT HERE"
        }
    }   
}

【问题讨论】:

    标签: ruby recursion hash


    【解决方案1】:

    这应该可以解决您的问题:

    def loop_params(schema)
      new_parent = Hash.new
    
      schema["fields"].each do |field|
        new_key = field["key"]
    
        if !field["group"].nil? && field["object"].nil?
          new_parent[new_key] = parse_config(field)
        else
          new_parent[new_key] = loop_params(field)
        end
      end
    
      new_parent
    end
    

    您的代码中有一些问题可能会导致错误。使用@ 声明变量使其成为实例变量,这意味着它的值将在函数完成后保持不变。由于我们看不到您的程序的其余部分,我不知道您是否需要全部或其中的任何部分,但我在没有它们的情况下编写了我的解决方案。

    还有几点需要注意:

    if fields["object"].nil? & !fields["group"].nil?
    

    您对& 的使用实际上是按位运算符and,而不是条件and。请改用&&

    另外,如果您使用puts 而不是printputs 会自动为您附加一个"\n",这很方便记住。

    【讨论】:

    • 您的解决方案就像一个魅力。感谢您提供有关我向您展示的代码 sn-p 的提示,我会在处理应用程序的其余部分时考虑这些提示。
    【解决方案2】:

    您可能想查看pretty_generate。请参阅“Ruby JSON.pretty_generate ... is pretty unpretty”和http://apidock.com/ruby/JSON/pretty_generate

    它需要一个哈希。

    您的扁平化字符串可能只需要再次使用JSON.parse 进行解析。

    【讨论】:

      猜你喜欢
      • 2018-11-07
      • 2013-03-14
      • 2013-07-08
      • 1970-01-01
      • 1970-01-01
      • 2012-01-09
      • 2012-03-27
      • 1970-01-01
      • 2014-06-24
      相关资源
      最近更新 更多