如何使用 Scala 將多個串列(具有相同數量的元素)轉換為 JSON 物件陣列?
鑒于:
List("Rec Type", "Evnt Type", "B/S Cd", "Sym"),
List("cmn_rec_type", "cmn_event_type_cd", "cmn_buy_sell_cd", "cmn_issue_sym_id"),
List("Record Type", "Event Type", "Buy Sell Code", "Issue Symbol ID"),
List("Common", "Common", "Common", "Common", "Common")
轉換為:
[
{
"name":"Rec Type",
"id":"cmn_rec_type",
"description": "Record Type",
"recordType":"Common"
},
{
"name":"Evnt Type",
"id":"cmn_event_type_cd",
"description":"Event Type",
"recordType":"Common"
},
{
"name":"B/S Cd",
"id":"cmn_buy_sell_cd",
"description":"Buy Sell Code",
"recordType":"Common"
},
{
"name":"Sym",
"id":"cmn_issue_sym_id",
"description":"Issue Symbol ID",
"recordType":"Common"
}
]
uj5u.com熱心網友回復:
這會起作用:
listOrLists.transpose.map { case name :: id :: description :: recordType :: _ =>
raw"""{ "name": "$name"
|, "id": "$id"
|, "description": "$description"
|, "recordType": "$recordType"
|}""".stripMargin
}.mkString("[", ",", "]")
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/467645.html