访问哈希和数组Ruby [英] Accessing hashes and arrays Ruby

查看:184
本文介绍了访问哈希和数组Ruby的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Rails应用程序中,我正在发送需要解码的复杂JSON字符串.那不是我知道的问题.

In my Rails application I'm sending a complex JSON string that needs to get decoded. That's not the problem I know how.

现在,在实现所有功能之前,我试图访问一些示例JSON结构,以查看是否可以访问所有变量.问题在于名称可以是可变的.

Now before I implement everything I'm trying to access some example JSON structures to see if I can get to all the variables. The problem is that the names can be variable.

{"configurations" : [ 
{ "drinks" : [
        {"menus" : [
          { "hot" : [
            {"id":15,"unit":"0.33", "price":"1", "currency":"Euro", "position": 4},
            {"id":15,"unit":"0.33", "price":"1", "currency":"Euro", "position": 6}
          ] },

          { "cold" : [
        {"id":15,"unit":"0.33", "price":"1", "currency":"Euro", "position": 4},
            {"id":15,"unit":"0.33", "price":"1", "currency":"Euro", "position": 6}
           ] },

          { "terminals" : [ {"id" : 4}, {"id": 6}, {"id": 7} ] }, 

          { "keys" : { "debit" : "on", "credit": "off" }  }

        ] }
] } ] } 

好吧,现在以下字段是可变的:饮料",热",冷" .所有其他字段将被称为相同.

Ok, now the following fields are variable: "drinks", "hot", "cold". All the other fields will be called the same.

现在,在解码后,我想访问此JSON字符串中的每个变量.在实现它之前,我尝试了一个简单的JSON:

Now I would like to access every variable in this JSON string after I decoded it. Before implementing it, I tryed a simple JSON:

{"configuration" : [ { "drinks" : [ { "id":15, "unit":"0.33" } ] } ] }

在rails中解码后导致

After decoding in rails resulting in

{ "configuration" => [{"drinks" => [{"id" => 15, "unit" => "0.33" }]}]}

现在如何不使用单词"drinks"访问例如idunit.该解决方案还应该可扩展到上面的示例.

Now how can I access for example id and unit without using the word "drinks". The solution should also be scalable to the example above.

一些额外的信息:在大JSON中,我应该访问其中列出的所有项目(标识),将它们保存到表中并返回新的标识,然后将其重新插入JSON中. (解释为什么需要这样做将花费额外的页面或4 ^^).

Some extra information: In the large JSON I should access all the items listed there (the id's) save them to a table and return the new id and then insert it back in the JSON. (explaining why this needs to be done will take an extra page or 4 ^^ ).

推荐答案

我写的是在Hash中进行深层搜索,但首先恐怕json出了点问题,当您解析它时,您会得到这个信息,您会注意到,例如终端未显示所有键.该代码仍必须修改为返回数组或哈希自身.

i wrote this to do a deep seek in a Hash, but first something is wrong with your json i'm afraid, when you parse it you get this, you'll notice that eg terminals doesn't show all the keys. The code still has to be adapted to return an array or hash itself.

hash = JSON.parse(string) #the string you published
=>

{"configurations"=>
  [{"drinks"=>
     [{"menus"=>
        [{"hot"=>
           [{"id"=>15,
             "unit"=>"0.33",
             "price"=>"1",
             "currency"=>"Euro",
             "position"=>4},
            {"id"=>15,
             "unit"=>"0.33",
             "price"=>"1",
             "currency"=>"Euro",
             "position"=>6}]},
         {"cold"=>
           [{"id"=>15,
             "unit"=>"0.33",
             "price"=>"1",
             "currency"=>"Euro",
             "position"=>4},
            {"id"=>15,
             "unit"=>"0.33",
             "price"=>"1",
             "currency"=>"Euro",
             "position"=>6}]},
         {"terminals"=>{"id"=>7}},
         {"keys"=>{"debit"=>"on", "credit"=>"off"}}]}]}]}

代码在这里

class Hash
  def dseek(search_key = "", path = "")
    self.each do|key, value|
      if value.is_a?(Hash)
        path += "#{key}."
        value.dseek(search_key, path)
      else
        if value.is_a?(Array)
          path += "#{key}."
          value.each do |val|
            val.dseek(search_key, path)
          end
        else
          puts "#{path}#{key}:#{value}" if search_key === key || search_key === ""
        end
      end
    end
  end
end


hash.dseek

给予

configurations.drinks.menus.hot.id:15
configurations.drinks.menus.hot.unit:0.33
configurations.drinks.menus.hot.price:1
configurations.drinks.menus.hot.currency:Euro
configurations.drinks.menus.hot.position:4
configurations.drinks.menus.hot.id:15
configurations.drinks.menus.hot.unit:0.33
configurations.drinks.menus.hot.price:1
configurations.drinks.menus.hot.currency:Euro
configurations.drinks.menus.hot.position:6
configurations.drinks.menus.cold.id:15
configurations.drinks.menus.cold.unit:0.33
configurations.drinks.menus.cold.price:1
configurations.drinks.menus.cold.currency:Euro
configurations.drinks.menus.cold.position:4
configurations.drinks.menus.cold.id:15
configurations.drinks.menus.cold.unit:0.33
configurations.drinks.menus.cold.price:1
configurations.drinks.menus.cold.currency:Euro
configurations.drinks.menus.cold.position:6
configurations.drinks.menus.terminals.id:7
configurations.drinks.menus.keys.debit:on
configurations.drinks.menus.keys.credit:off

hash.dseek("id")

给予

configurations.drinks.menus.hot.id:15
configurations.drinks.menus.hot.id:15
configurations.drinks.menus.cold.id:15
configurations.drinks.menus.cold.id:15
configurations.drinks.menus.terminals.id:7

这篇关于访问哈希和数组Ruby的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆