如何使用 Scala 将多个列表解析为 json 个对象的数组?
How to parse multiple lists into array of json objects, using Scala?
如何使用 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"
}
]
这可行:
listOrLists.transpose.map { case name :: id :: description :: recordType :: _ =>
raw"""{ "name": "$name"
|, "id": "$id"
|, "description": "$description"
|, "recordType": "$recordType"
|}""".stripMargin
}.mkString("[", ",", "]")
如何使用 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"
}
]
这可行:
listOrLists.transpose.map { case name :: id :: description :: recordType :: _ =>
raw"""{ "name": "$name"
|, "id": "$id"
|, "description": "$description"
|, "recordType": "$recordType"
|}""".stripMargin
}.mkString("[", ",", "]")