进行 JSON 解析时以逗号分隔的输出
Comma separated output while doing JSON parse
当我尝试使用以下方法解析 JSON 值时,得到的输出为:
2017-03-28T13:07:46Z
,
rfw23wmnq4wd
但是,我想避免输出中的下一行,我希望输出是
2017-03-28T13:07:46Z , rfw23wmnq4wd.
对ruby完全陌生,请不要介意,因为它是非常基础的
require 'json'
value = `curl https://api.statuspage.io/v1/pages/pageid/incidents.json -H "Authorization: OAuth APIKEY"`
#puts value
data_hash = JSON.parse(value).map {|h| [h["created_at"] , "," , h["id"]] }
puts data_hash
此外,当我试图获取 body
的值时,没有得到 nay 输出,请协助
{{“id":"920h1l69bqgy","components":[{"id":"230g793b5kry","page_id":"3h5p5rr839fk","group_id":"69pspx7kvhhj","created_at":"2016-08-11T12:20:00Z","updated_at":"2018-10-01T18:14:33Z","group":false,"name":"WEB APP","description":null,"position":4,"status":"operational","showcase":false,"only_show_if_degraded":false}],"created_at":"2018-10-01T17:18:59Z","impact":"minor","impact_override":null,"incident_updates":[{"id":"dwwk370t321t","incident_id":"920h1l69bqgy","affected_components":[{"code":"230g793b5kry","name":"EU CENTRAL(FRANKFURT) - WEB APP","old_status":"degraded_performance","new_status":"operational"}],"body":"All connectivity issues on the EU accounts have been resolved and the portals will now load without any hassles.","created_at":"2018-10-01T18:14:33.844Z","custom_tweet":null,"deliver_notifications":true,"display_at":"2018-10-01T23:44:33.844+05:30","status":"resolved","tweet_id":null,"twitter_updated_at":null,"updated_at":"2018-10-01T18:14:33.844Z","wants_twitter_update":false}
当你对数组使用 puts 时,每个元素都打印在一个换行符中,你可以尝试这样打印,使用 string interpolation:
JSON.parse(value).map {|h| puts "#{h['created_at']} , #{h['id']}" }
当我尝试使用以下方法解析 JSON 值时,得到的输出为:
2017-03-28T13:07:46Z
,
rfw23wmnq4wd
但是,我想避免输出中的下一行,我希望输出是
2017-03-28T13:07:46Z , rfw23wmnq4wd.
对ruby完全陌生,请不要介意,因为它是非常基础的
require 'json'
value = `curl https://api.statuspage.io/v1/pages/pageid/incidents.json -H "Authorization: OAuth APIKEY"`
#puts value
data_hash = JSON.parse(value).map {|h| [h["created_at"] , "," , h["id"]] }
puts data_hash
此外,当我试图获取 body
的值时,没有得到 nay 输出,请协助
{{“id":"920h1l69bqgy","components":[{"id":"230g793b5kry","page_id":"3h5p5rr839fk","group_id":"69pspx7kvhhj","created_at":"2016-08-11T12:20:00Z","updated_at":"2018-10-01T18:14:33Z","group":false,"name":"WEB APP","description":null,"position":4,"status":"operational","showcase":false,"only_show_if_degraded":false}],"created_at":"2018-10-01T17:18:59Z","impact":"minor","impact_override":null,"incident_updates":[{"id":"dwwk370t321t","incident_id":"920h1l69bqgy","affected_components":[{"code":"230g793b5kry","name":"EU CENTRAL(FRANKFURT) - WEB APP","old_status":"degraded_performance","new_status":"operational"}],"body":"All connectivity issues on the EU accounts have been resolved and the portals will now load without any hassles.","created_at":"2018-10-01T18:14:33.844Z","custom_tweet":null,"deliver_notifications":true,"display_at":"2018-10-01T23:44:33.844+05:30","status":"resolved","tweet_id":null,"twitter_updated_at":null,"updated_at":"2018-10-01T18:14:33.844Z","wants_twitter_update":false}
当你对数组使用 puts 时,每个元素都打印在一个换行符中,你可以尝试这样打印,使用 string interpolation:
JSON.parse(value).map {|h| puts "#{h['created_at']} , #{h['id']}" }