[英]Return a JSON response as CSV file from spring-boot controller
在我的 spring-boot 項目中,我以 JSON 格式從外部 API 獲取信息。 響應表示如下:
{
"id":237,
"first_name":"LeBron",
"last_name":"James",
"position":"F",
"height_feet": 6,
"height_inches": 8,
"weight_pounds": 250,
"team":{
"id":14,
"abbreviation":"LAL",
"city":"Los Angeles",
"conference":"West",
"division":"Pacific",
"full_name":"Los Angeles Lakers",
"name":"Lakers"
}
}
我的任務是從這個 JSON 響應中返回一個 CSV 文件。 我在 inte.net 上尋找一些信息,只能找到從常規 JSON 到 CSV 的轉換,但我得到的 JSON 響應是嵌套的,轉換不起作用。 我怎樣才能讓它發生? 我應該怎么辦? 任何幫助將不勝感激。
一種方法是預處理 JSON 數據並將其轉換為平面 JSON 結構。 您可以編寫自己的方法來執行此操作,也可以使用 JOLT 庫來執行此操作:
樣本規格
[
{
"operation": "shift",
"spec": {
"*": "&",
"team": {
"id": "team-id",
"abbreviation": "team-abbreviation",
"city": "team-city",
"conference": "team-conference",
"division": "team-division",
"full_name": "team-full-name",
"name": "team-name"
}
}
}
]
將 JSON 轉換為
{
"id" : 237,
"first_name" : "LeBron",
"last_name" : "James",
"position" : "F",
"height_feet" : 6,
"weight_pounds" : 250,
"team-id" : 14,
"team-abbreviation" : "LAL",
"team-city" : "Los Angeles",
"team-conference" : "West",
"team-division" : "Pacific",
"team-full-name" : "Los Angeles Lakers",
"team-name" : "Lakers"
}
您可以在此處閱讀有關 JOLT 的更多信息 - https://github.com/bazaarvoice/jolt#Demo
您還可以在他們在 http 創建的演示頁面上實時測試您的規范: http://jolt-demo.appspot.com/#inception
編輯:在閱讀了更多文檔之后——這里是規范的一個較短版本,它將實現與上面給出的結果類似的結果:
[
{
"operation": "shift",
"spec": {
"*": "&",
"team": {
"*": "team-&"
}
}
}
]
您可以使用庫Josson來完成這項工作。
https://github.com/octomix/josson
你的例子是 JSON object
Josson object = Josson.fromJsonString(
"{" +
" \"id\":237," +
" \"first_name\":\"LeBron\"," +
" \"last_name\":\"James\"," +
" \"position\":\"F\"," +
" \"height_feet\": 6," +
" \"height_inches\": 8," +
" \"weight_pounds\": 250," +
" \"team\":{" +
" \"id\":14," +
" \"abbreviation\":\"LAL\"," +
" \"city\":\"Los Angeles\"," +
" \"conference\":\"West\"," +
" \"division\":\"Pacific\"," +
" \"full_name\":\"Los Angeles Lakers\"," +
" \"name\":\"Lakers\"" +
" }" +
"}");
轉型
String keys = object.getString("flatten('.','[%d]').keys().csv()");
System.out.println(keys);
String values = object.getString("flatten('.','[%d]').csv()");
System.out.println(values);
Output
id,first_name,last_name,position,height_feet,height_inches,weight_pounds,team.id,team.abbreviation,team.city,team.conference,team.division,team.full_name,team.name
237,LeBron,James,F,6,8,250,14,LAL,Los Angeles,West,Pacific,Los Angeles Lakers,Lakers
如果輸入是一個 JSON 數組
Josson array = Josson.fromJsonString(
"[{" +
" \"id\":237," +
" \"first_name\":\"LeBron\"," +
" \"last_name\":\"James\"," +
" \"position\":\"F\"," +
" \"height_feet\": 6," +
" \"height_inches\": 8," +
" \"weight_pounds\": 250," +
" \"team\":{" +
" \"id\":14," +
" \"abbreviation\":\"LAL\"," +
" \"city\":\"Los Angeles\"," +
" \"conference\":\"West\"," +
" \"division\":\"Pacific\"," +
" \"full_name\":\"Los Angeles Lakers\"," +
" \"name\":\"Lakers\"" +
" }" +
"}," +
"{" +
" \"id\":888," +
" \"first_name\":\"Anthony\"," +
" \"last_name\":\"Davis\"," +
" \"position\":\"F\"," +
" \"height_feet\": 6," +
" \"height_inches\": 10," +
" \"weight_pounds\": 253," +
" \"team\":{" +
" \"id\":14," +
" \"abbreviation\":\"LAL\"," +
" \"city\":\"Los Angeles\"," +
" \"conference\":\"West\"," +
" \"division\":\"Pacific\"," +
" \"full_name\":\"Los Angeles Lakers\"," +
" \"name\":\"Lakers\"" +
" }" +
"}]");
轉型
String keys = array.getString("[0].flatten('.','[%d]').keys().csv()");
System.out.println(keys);
String values = array.getString("[]@.flatten('.','[%d]').csv().@join('\n')");
System.out.println(values);
Output
id,first_name,last_name,position,height_feet,height_inches,weight_pounds,team.id,team.abbreviation,team.city,team.conference,team.division,team.full_name,team.name
237,LeBron,James,F,6,8,250,14,LAL,Los Angeles,West,Pacific,Los Angeles Lakers,Lakers
888,Anthony,Davis,F,6,10,253,14,LAL,Los Angeles,West,Pacific,Los Angeles Lakers,Lakers
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.