解析 JSON 输出时输出 Space

Space in output while parsing JSON output

我从以下 curl 命令的 JSON 输出中解析了值(感谢 Whosebug),

1.) 但我在输出中看到很好的 spaces(附上示例图像以供参考)。需要输出没有额外的spaces(在输出末尾看到额外的spaces,请参考图片)。请帮助解决这个问题,

(ruby 很新,很抱歉基础知识)

space after last line of output

2.)每个值后还需要space .

样本输出:

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