新手问题 如何取出数组中某个元素的特定字段值?

GSTA_Yalo · 2018年07月04日 · 最后由 ruby_p 回复于 2018年07月05日 · 5504 次阅读

现在有一个数组 abc 如下: [{\"alarmSeverity\"=>\"\", \"securityName\"=>\"\", \"authPass\"=>\"\", \"ip\"=>nil, \"resources\"=>[\"9999994\", \"9999990\", \"9999991\", \"9999992\", \"9999993\"], \"updateTime\"=>\"2018-07-04 16:06:41\", \"community\"=>\"\", \"type\"=>nil, \"version\"=>\"2c\", \"authProtocol\"=>\"\", \"alarmType\"=>\"\", \"createTime\"=>\"2018-07-04 16:06:41\", \"port\"=>nil, \"name\"=>\"1\", \"privProtocol\"=>\"\", \"id\"=>\"bd41966778b84176a621bcd326d5f357\", \"privPass\"=>\"\", \"resourceType\"=>\"1\"}, {\"alarmSeverity\"=>\"\", \"securityName\"=>\"\", \"authPass\"=>\"\", \"ip\"=>nil, \"resources\"=>[\"9999994\", \"9999990\", \"9999991\", \"9999992\", \"9999993\"], \"updateTime\"=>\"2018-07-04 16:06:22\", \"community\"=>\"\", \"type\"=>nil, \"version\"=>\"\", \"authProtocol\"=>\"\", \"alarmType\"=>\"\", \"createTime\"=>\"2018-07-04 16:06:22\", \"port\"=>nil, \"name\"=>\"\", \"privProtocol\"=>\"\", \"id\"=>\"7c6c388e120648749edb86c3ca70a946\", \"privPass\"=>\"\", \"resourceType\"=>\"1\"}, {\"alarmSeverity\"=>\"1\", \"securityName\"=>\"test\", \"authPass\"=>\"testpwd1\", \"ip\"=>nil, \"resources\"=>[\"9999994\", \"9999990\", \"9999991\", \"9999992\", \"9999993\"], \"updateTime\"=>\"2018-07-04 15:06:55\", \"community\"=>\"public\", \"type\"=>nil, \"version\"=>\"v3\", \"authProtocol\"=>\"SHA\", \"alarmType\"=>\"1\", \"createTime\"=>\"2018-07-04 15:06:55\", \"port\"=>nil, \"name\"=>\"test_subscribe\", \"privProtocol\"=>\"加密 RSA\", \"id\"=>\"b3b82abf62124d0d90f7098d6bca9ec1\", \"privPass\"=>\"testpwd2\", \"resourceType\"=>\"\"},

我想将 abc 中的 id 取出来的话,我用的是 abc.id 的方式,但是报错:NoMethodError: undefined method `id' for #<Array:,是什么原因?

abc.map do |i|
  i['id']  
end

eval

def fetch_value(column)
  values = []
  parse_str =  "[{\"alarmSeverity\"=>\"\", \"securityName\"=>\"\", \"authPass\"=>\"\", \"ip\"=>nil, \"resources\"=>[\"9999994\", \"9999990\", \"9999991\", \"9999992\", \"9999993\"], \"updateTime\"=>\"2018-07-04 16:06:41\", \"community\"=>\"\", \"type\"=>nil, \"version\"=>\"2c\", \"authProtocol\"=>\"\", \"alarmType\"=>\"\", \"createTime\"=>\"2018-07-04 16:06:41\", \"port\"=>nil, \"name\"=>\"1\", \"privProtocol\"=>\"\", \"id\"=>\"bd41966778b84176a621bcd326d5f357\", \"privPass\"=>\"\", \"resourceType\"=>\"1\"}, {\"alarmSeverity\"=>\"\", \"securityName\"=>\"\", \"authPass\"=>\"\", \"ip\"=>nil, \"resources\"=>[\"9999994\", \"9999990\", \"9999991\", \"9999992\", \"9999993\"], \"updateTime\"=>\"2018-07-04 16:06:22\", \"community\"=>\"\", \"type\"=>nil, \"version\"=>\"\", \"authProtocol\"=>\"\", \"alarmType\"=>\"\", \"createTime\"=>\"2018-07-04 16:06:22\", \"port\"=>nil, \"name\"=>\"\", \"privProtocol\"=>\"\", \"id\"=>\"7c6c388e120648749edb86c3ca70a946\", \"privPass\"=>\"\", \"resourceType\"=>\"1\"}, {\"alarmSeverity\"=>\"1\", \"securityName\"=>\"test\", \"authPass\"=>\"testpwd1\", \"ip\"=>nil, \"resources\"=>[\"9999994\", \"9999990\", \"9999991\", \"9999992\", \"9999993\"], \"updateTime\"=>\"2018-07-04 15:06:55\", \"community\"=>\"public\", \"type\"=>nil, \"version\"=>\"v3\", \"authProtocol\"=>\"SHA\", \"alarmType\"=>\"1\", \"createTime\"=>\"2018-07-04 15:06:55\", \"port\"=>nil, \"name\"=>\"test_subscribe\", \"privProtocol\"=>\"加密RSA\", \"id\"=>\"b3b82abf62124d0d90f7098d6bca9ec1\", \"privPass\"=>\"testpwd2\", \"resourceType\"=>\"\"}]"
  result = eval parse_str
  result.each{|temp| values.push(temp[column])}
  values
end
p fetch_value('id')
easonlovewan 回复

不单独新建一个方法的话,怎么做?现在这个数组 abc 已经赋值给一个局部变量,具体代码如下:

def index
@abc = 上述数组
id = @abc.id
end

报错 NoMethodError: undefined method `id' for #<Array:

GSTA_Yalo 回复

你这个串是符合 json 标准的字符串,并不是一个数组对象。你需要把它解析成 ruby 对象,这个对象就是你要遍历取值的数组。 是我上面写的不够清晰吗😆另外你上一条帖子也是同类的问题

easonlovewan 回复

这个串是一个数组对象,已用@abc.class查看过。

GSTA_Yalo 回复

数组对象可以直接 abc.id 的吗

GSTA_Yalo 关闭了讨论。 07月05日 15:49
GSTA_Yalo 重新开启了讨论。 07月05日 15:51
需要 登录 后方可回复, 如果你还没有账号请 注册新账号