这是我的json文件
{
"osNameSpace" : "prod",
"bucketNames" : [ "RC_osbackup_config_d0007cz_idfdafa3", "IC_osbackup_vman_d0007cz_dasdc3w" ],
"user" : "RC_osbackup_svc_user_d0007cz_dasf2fd",
"userOcid" : "rcid1.user.oc1..aaaaaaaazf5e27hbpst2pxarisi5qvkmgotwm5ro4nhjkkjq4mmxengckv2a",
"group" : "RC_osbackup_svc_grp_d0007cz_asdf24"
}
我想搜索字符串'osbackup_config'并分配给'os_conf'变量。字符串'osbackup_vman'与'os_vman'类似
我尝试了以下方法,但是没有运气
input_file="/root/testjson_2.json"
r = JSON.parse(File.read(input_file))
r["bucketNames"].find {|r1| r1['dbbackup_rman']=='rman'}['rman_bucket_name']
r["bucketNames"].find {|r1| r1['dbbackup_config']=='config'}['config_bucket_name']
答案 0 :(得分:1)
为什么不呢?
r['bucketNames'].find { |v| v.include?'osbackup_config' }
或
os_conf = r['bucketNames'].find { |v| v['osbackup_config'] }
osbackup_vman = r['bucketNames'].find { |v| v['osbackup_vman'] }