我从下面 curl 命令的 JSON 输出中解析值(感谢 stackoverflow),
1.) 但我在输出中看到很大的空格(附上示例图像以供参考)。需要没有多余空格的输出(在输出末尾看到多余的空格,请参考图片)。请对此提供帮助,
(对于基本的东西,ruby 很新表示歉意)
2.) 每个值后也需要空格。
样品输出:
ID : 7j6rzn1r43zz , CREATED AT : 2017-04-03T12:08:03Z , LINK : http://stspg.io/5Es5 , ISSUE NAME : Intermittent Issue , DESCRIPTION : There is a minor performance degradation in our app for some customers in US, We are working on it , STATUS : identified ,DESCRIPTION : We have resolved the performance issue in our app, We are closely monitoring it. , STATUS : resolved
ID : g8tk0jtvgybt , CREATED AT : 2017-04-01T11:11:27Z , LINK : http://stspg.io/5EHd , ISSUE NAME : Intermittent Issue , DESCRIPTION : Currently we are facing delay in incoming emails as we have problem with our email service provider. We are working on it. , STATUS : investigating ,DESCRIPTION : The delay in incoming emails issue has been resolved now. Application is working fine. , STATUS : resolved
预期的
ID : 7j6rzn1r43zz , CREATED AT : 2017-04-03T12:08:03Z , LINK : http://stspg.io/5Es5 , ISSUE NAME : Intermittent Issue , DESCRIPTION : There is a minor performance degradation in our app for some customers in US, We are working on it , STATUS : identified ,DESCRIPTION : We have resolved the performance issue in our app, We are closely monitoring it. , STATUS : resolved
ID : g8tk0jtvgybt , CREATED AT : 2017-04-01T11:11:27Z , LINK : http://stspg.io/5EHd , ISSUE NAME : Intermittent Issue , DESCRIPTION : Currently we are facing delay in incoming emails as we have problem with our email service provider. We are working on it. , STATUS : investigating ,DESCRIPTION : The delay in incoming emails issue has been resolved now. Application is working fine. , STATUS : resolved
卷曲:
def incidents
value = `curl https://api.statuspage.io/v1/pages/incidents.json -H "Authorization: OAuth a8ef"`
data_hash = JSON.parse(value).map {|h| puts "ID : #{h["id"]} , CREATED AT : #{h["created_at"]} , LINK : #{h["shortlink"]} , ISSUE NAME : #{h["name"]} , DESCRIPTION : #{h["incident_updates"][1]["body"]} , STATUS : #{h["incident_updates"][1]["status"]} ,DESCRIPTION : #{h["incident_updates"][0]["body"]} , STATUS : #{h["incident_updates"][0]["status"]}"}
puts data_hash
end
您在map
迭代器中执行输出,然后执行附加puts
. 这是没有必要的,正如您所看到的,会导致不必要的输出。
替换map
为each
并删除最后一个puts
。类似的东西:
JSON.parse(value).each do |h|
puts "ID : #{h["id"]} , CREATED AT : #{h["created_at"]} , LINK : #{h["shortlink"]} , ISSUE NAME : #{h["name"]} , DESCRIPTION : #{h["incident_updates"][1]["body"]} , STATUS : #{h["incident_updates"][1]["status"]} ,DESCRIPTION : #{h["incident_updates"][0]["body"]} , STATUS : #{h["incident_updates"][0]["status"]}"
end
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句