Ruby中的Ruby返回值

时间:2016-11-08 06:14:06

标签: arrays ruby ruby-on-rails-4

在我的ruby控制台中,a = first

=> <Ng::EntityConfiguration id: 15881, entity_id: 1, entity_type: "Ng::Company", key: "wpa2.psk", value: "[{"ssid":"LVL6-Staff","password":"987654321", created_at: "2016-11-08 05:13:04", updated_at: "2016-11-08 05:13:04", name: "WIFI/Level 6">

然后a.value

会给你=> "[{"ssid":"LVL6-Staff","password":"987654321","dhcp":"Enabled"}]"

我的问题是,当对象是密码时如何获取值?因为当我尝试a.value[:password]时出错了。

1 个答案:

答案 0 :(得分:2)

>> x = JSON.parse("[{\"ssid\":\"LVL6-Staff\",\"password\":\"987654321\",\"dhcp\":\"Enabled\"}]") => [{"ssid"=>"LVL6-Staff", "password"=>"987654321", "dhcp"=>"Enabled"}] >> x.last['password'] => "987654321" 是带有哈希值的数组之后,它是一个JSON字符串。

{
   "size": 0,
   "query": {
      "term": {
         "title": {
            "value": "apple pie"
         }
      }
   },
   "aggs": {
      "titleAgg": {
         "terms": {
            "field": "title",
            "order": {
               "scoreAgg": "desc"
            }
         },
         "aggs": {
            "scoreAgg": {
               "max": {
                  "script": "_score"
               }
            }
         }
      }
   }
}