1
有沒有人有任何示例Groovy代碼將JSON文檔轉換爲CSV文件?我試圖在谷歌上搜索,但無濟於事。將json轉換爲CSV文件的Groovy代碼
例輸入(從評論):
[ company_id: '1',
web_address: 'vodafone.com/',
phone: '+44 11111',
fax: '',
email: '',
addresses: [
[ type: "office",
street_address: "Vodafone House, The Connection",
zip_code: "RG14 2FN",
geo: [ lat: 51.4145, lng: 1.318385 ] ]
],
number_of_employees: 91272,
naics: [
primary: [
"517210": "Wireless Telecommunications Carriers (except Satellite)" ],
secondary: [
"517110": "Wired Telecommunications Carriers",
"517919": "Internet Service Providers",
"518210": "Web Hosting"
]
]
從編輯更多信息:
def export(){
def exportCsv = [ [ id:'1', color:'red', planet:'mars', description:'Mars, the "red" planet'],
[ id:'2', color:'green', planet:'neptune', description:'Neptune, the "green" planet'],
[ id:'3', color:'blue', planet:'earth', description:'Earth, the "blue" planet'],
]
def out = new File('/home/mandeep/groovy/workspace/FirstGroovyProject/src/test.csv')
exportCsv.each {
def row = [it.id, it.color, it.planet,it.description]
out.append row.join(',')
out.append '\n'
}
return out
}
這將定製到您的json,因爲沒有規定json數組中的對象必須包含相同的屬性。你有一些例子json嗎?你有什麼嘗試?你有什麼困難? –
我有String JSON數據。我想使用groovy代碼將此json數據轉換爲csv文件。 像這裏我創建了csv文件與靜態數據,但我想從字符串json對象創建csv文件,並希望發送此csv文件作爲附件使用發送電子郵件代碼在groovy –
您的問題顯而易見,你有一些Json和想要創建一個CSV,但你有什麼JSON?任何解決方案對您的json都是獨一無二的,因爲Json可以採取各種形式,並且不必輕易地匹配csv格式。你可以將一些例子Json作爲編輯發佈到你的問題中嗎? –