进行JSON解析时以逗号分隔的输出

时间:2018-10-03 17:08:50

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

当我尝试使用以下方法解析JSON值时,输出为:

2017-03-28T13:07:46Z
,
rfw23wmnq4wd

但是,我想避免输出中的下一行,我希望输出为

2017-03-28T13:07:46Z , rfw23wmnq4wd.

红宝石完全陌生,请不要介意,因为它是非常基础的

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}

1 个答案:

答案 0 :(得分:1)

将puts与数组一起使用时,每个元素都以换行符打印,您可以尝试使用string interpolation进行打印:

JSON.parse(value).map {|h| puts "#{h['created_at']} , #{h['id']}" }