我有这个json。
{"schedule"=>
"schedule_layers"=>
[{"name"=>"Layer 1",
"rendered_schedule_entries"=>[],
"rendered_coverage_percentage"=>nil,
"id"=>"ABC123",
"start"=>"October3",
"end"=>nil,
"rotation_virtual_start"=>"October4",
"rotation_turn_length_seconds"=>500000,
"users"=>
[{"user"=>
{"id"=>"11111111",
"type"=>"user_reference",
"summary"=>"Micheal.Chi",
"self"=>"https://api.pagerduty.com/",
"html_url"=>"https://pagerduty.com/"}},
{"user"=>
{"id"=>"22222222",
"type"=>"user_reference",
"summary"=>"Ash.A.Basha",
"self"=>"https://api.pagerduty.com/",
"html_url"=>"https://pagerduty.com/"}},
{"user"=>
{"id"=>"33333333",
"type"=>"user_reference",
"summary"=>"Ven.Doli",
"self"=>"https://api.pagerduty.com/",
"html_url"=>"https://pagerduty.com/"}},
{"user"=>
{"id"=>"44444444",
"type"=>"user_reference",
"summary"=>"Abby.Silly",
"self"=>"https://api.pagerduty.com/",
"html_url"=>"https://pagerduty.com/"}}],
{"name"=>"Layer 2",
"rendered_schedule_entries"=>[],
"rendered_coverage_percentage"=>nil,
"id"=>"BHD432",
"start"=>"November4",
"end"=>"November5",
"rotation_virtual_start"=>"November4",
"rotation_turn_length_seconds"=>600000,
"users"=>
[{"user"=>
{"id"=>"5555555",
"type"=>"user_reference",
"summary"=>"Hart.Shu",
"self"=>nil,
"html_url"=>nil}},
{"user"=>
{"id"=>"66666666",
"type"=>"user_reference",
"summary"=>"Man.Sam",
"self"=>nil,
"html_url"=>nil}},
{"user"=>
{"id"=>"77777777",
"type"=>"user_reference",
"summary"=>"Lie.Sigh",
"self"=>"https://api.pagerduty.com/users/PRW1P1J",
"html_url"=>"https://target.pagerduty.com/users/PRW1P1J"}},
{"user"=>
{"id"=>"88888888",
"type"=>"user_reference",
"summary"=>"Cry.Bye",
"self"=>"https://api.pagerduty.com/users/P9WCM8S",
"html_url"=>"https://target.pagerduty.com/users/P9WCM8S"}},
{"user"=>
{"id"=>"99999999",
"type"=>"user_reference",
"summary"=>"Man.Lan",
"self"=>nil,
"html_url"=>nil}},
{"user"=>
{"id"=>"00000000",
"type"=>"user_reference",
"summary"=>"Large.Hurry",
"self"=>"https://api.pagerduty.com/users/PEA0OXF",
"html_url"=>"https://target.pagerduty.com/users/PEA0OXF"}},
{"user"=>
{"id"=>"12345678",
"type"=>"user_reference",
"summary"=>"James.Harden",
"self"=>"https://api.pagerduty.com/users/PFOE8GN",
"html_url"=>"https://target.pagerduty.com/users/PFOE8GN"}},
{"user"=>
{"id"=>"8765321",
"type"=>"user_reference",
"summary"=>"Tommy.Tucker",
"self"=>"https://api.pagerduty.com/users/PAPSIMG",
"html_url"=>"https://target.pagerduty.com/users/PAPSIMG"}},
{"user"=>
{"id"=>"0101010",
"type"=>"user_reference",
"summary"=>"Code.Re",
"self"=>"https://api.pagerduty.com/users/P8AVASJ",
"html_url"=>"https://target.pagerduty.com/users/P8AVASJ"}},
{"user"=>
{"id"=>"21212121",
"type"=>"user_reference",
"summary"=>"Manny.Spinny",
"self"=>nil,
"html_url"=>nil}},
{"user"=>
{"id"=>"41414141",
"type"=>"user_reference",
"summary"=>"Snit.Cimen",
"self"=>"https://api.pagerduty.com/users/PPTMCT1",
"html_url"=>"https://target.pagerduty.com/users/PPTMCT1"}},
{"user"=>
{"id"=>"52525252",
"type"=>"user_reference",
"summary"=>"Dian.Ost",
"self"=>"https://api.pagerduty.com/users/PAOJEX4",
"html_url"=>"https://target.pagerduty.com/users/PAOJEX4"}},
{"user"=>
{"id"=>"65656565",
"type"=>"user_reference",
"summary"=>"Kev.Shy",
"self"=>"https://api.pagerduty.com/users/P6YK4Y4",
"html_url"=>"https://target.pagerduty.com/users/P6YK4Y4"}}],
"restrictions"=>[]},
{"name"=>"Layer 1",
"rendered_schedule_entries"=>[],
"rendered_coverage_percentage"=>nil,
"id"=>"P6VKKZX",
"start"=>"2017-10-09",
"end"=>nil,
"rotation_virtual_start"=>"2017-10-08",
"rotation_turn_length_seconds"=>500505,
"users"=>
[{"user"=>
{"id"=>"78787878",
"type"=>"user_reference",
"summary"=>"Lie.N",
"self"=>"https://api.pagerduty.com/",
"html_url"=>"https://pagerduty.com/"}},
{"user"=>
{"id"=>"56565656",
"type"=>"user_reference",
"summary"=>"Habibi Le",
"self"=>"https://api.pagerduty.com/",
"html_url"=>"https://pagerduty.com/"}},
{"user"=>
{"id"=>"32233223",
"type"=>"user_reference",
"summary"=>"Large.Pop",
"self"=>"https://api.pagerduty.com/",
"html_url"=>"https://pagerduty.com/"}},
{"user"=>
{"id"=>"09090909",
"type"=>"user_reference",
"summary"=>"Timmy.Nitt",
"self"=>"https://api.pagerduty.com/",
"html_url"=>"https://target.pagerduty.com/"}},
{"user"=>
{"id"=>"12435676",
"type"=>"user_reference",
"summary"=>"Bri.Barry",
"self"=>"https://api.pagerduty.com/",
"html_url"=>"https://pagerduty.com/"}},
{"user"=>
{"id"=>"10101010",
"type"=>"user_reference",
"summary"=>"Dan Ostr",
"self"=>"https://api.pagerduty.com/",
"html_url"=>"https://pagerduty.com/"}},
{"user"=>
{"id"=>"ABDUCDDD",
"type"=>"user_reference",
"summary"=>"Ive.Chlong",
"self"=>"https://api.pagerduty.com/",
"html_url"=>"https://pagerduty.com/"}},
{"user"=>
{"id"=>"OHGILKAS",
"type"=>"user_reference",
"summary"=>"Jiff.Loss",
"self"=>"https://api.pagerduty.com/",
"html_url"=>"https://pagerduty.com/"}},
{"user"=>
{"id"=>"AFRJIDN",
"type"=>"user_reference",
"summary"=>"Levy Shy",
"self"=>"https://api.pagerduty.com/",
"html_url"=>"https://pagerduty.com/"}},
{"user"=>
{"id"=>"OFJDKANF",
"type"=>"user_reference",
"summary"=>"Patty Swice",
"self"=>"https://api.pagerduty.com/",
"html_url"=>"https://pagerduty.com/"}}],
"restrictions"=>
[{"type"=>"daily_restriction",
"start_time_of_day"=>"08:30:00",
"duration_seconds"=>120000}]}],
"overrides_subschedule"=>
{"name"=>"Overrides",
"rendered_schedule_entries"=>[]
我的任务是只根据时间表获取摘要=> schedule_layers => users =>用户。这是我检索数据的代码。
somefile = File.open("out.txt", "w+")
jdoc.fetch("schedule").fetch("schedule_layers").each do |jsonUser|
somefile.puts jsonUser["users"][0]["user"]["summary"]
somefile.puts jsonUser["users"][1]["user"]["summary"]
somefile.puts jsonUser["users"][2]["user"]["summary"]
somefile.puts jsonUser["users"][3]["user"]["summary"]
somefile.puts "_" * 80
end
我意识到用户哈希有一个数组,这就是为什么我有那些奇怪的数字。无论我的文本文件在运行此代码后显示如此。
Micheal.Chi
Ashraf.A.Pasha
Ven.Doli
Abby.Silly
__________________________________________________________
Hart.Shu
Man.Sam
Lie.Sigh
Cry.Bye
__________________________________________________________
Lie.N
Habibi Le
Large.Pop
Timmy.Nitt
__________________________________________________________
我理解为什么我每个部分只得到四个。但我想找到真正的代码,以最美化的方式获取这些价值。
答案 0 :(得分:0)
不完全确定你想要什么,但我认为这可能就是它。
somefile = File.open("out.txt", "w+")
jdoc.fetch("schedule").fetch("schedule_layers").each do |layer|
layer["users"].each do |user|
somefile.puts user["user"]["summary"]
end
somefile.puts "_" * 80
end
使用each迭代每个图层中的每个用户对象