Rubyは初めてで、質問がありました。JSONをCSVに変換する.rbファイルを作成しようとしています。
私は私を作らせたいくつかの異なる情報源に出くわしました:
require "rubygems"
require 'fastercsv'
require 'json'
csv_string = FasterCSV.generate({}) do |csv|
JSON.parse(File.open("small.json").read).each do |hash|
csv << hash
end
end
puts csv_string
現在、実際にはテキストを出力しますが、スペースやコンマなどを使用せずにすべて一緒に押しつぶされます。CSVファイルをよりカスタマイズしてクリアにして、そのファイルをエクスポートできるようにするにはどうすればよいですか。
JSONは次のようになります。
{
"results": [
{
"reportingId": "s",
"listingType": "Business",
"hasExposureProducts": false,
"name": "Medeco Medical Centre World Square",
"primaryAddress": {
"geoCodeGranularity": "PROPERTY",
"addressLine": "Shop 9.01 World Sq Shopng Cntr 644 George St",
"longitude": "151.206172",
"suburb": "Sydney",
"state": "NSW",
"postcode": "2000",
"latitude": "-33.876416",
"type": "VANITY"
},
"primaryContacts": [
{
"type": "PHONE",
"value": "(02) 9264 8500"
}
]
},xxx
}
次のようなCSVがあります。
reportingId, s, listingType, Business, name, Medeco Medical...., addressLine, xxxxx, longitude, xxxx, latitude, xxxx, state, NSW, postcode, 2000, type, phone, value, (02) 92648544