进行 JSON 解析时的逗号分隔输出
Posted
技术标签:
【中文标题】进行 JSON 解析时的逗号分隔输出【英文标题】:Comma separated output while doing JSON parse 【发布时间】:2019-03-08 23:52:13 【问题描述】:当我尝试使用以下方法解析 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
的值时,没有得到任何输出,请协助
“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】:当您将 puts 与数组一起使用时,每个元素都以换行符打印,您可以尝试使用这种方式打印,使用 string interpolation:
JSON.parse(value).map |h| puts "#h['created_at'] , #h['id']"
【讨论】:
非常感谢,我在上面又添加了一个查询,请您帮忙 @KarthikrajSaravanan 在第一个问题得到令人满意的答案后,停止编辑和扩展问题。以上是关于进行 JSON 解析时的逗号分隔输出的主要内容,如果未能解决你的问题,请参考以下文章