简体   繁体   English

如何使用 Scala 将多个列表解析为 json 个对象的数组?

[英]How to parse multiple lists into array of json objects, using Scala?

How to convert multiple Lists (with equal number of elements) into Array of JSON Objects, using Scala?如何使用 Scala 将多个列表(具有相同数量的元素)转换为 JSON 对象的数组?

Given:鉴于:

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")

Convert into:转换为:

[
{
"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"
}
]

This would work:这会起作用:

listOrLists.transpose.map { case name :: id :: description :: recordType :: _ =>
  raw"""{ "name": "$name"
       |, "id": "$id"
       |, "description": "$description"
       |, "recordType": "$recordType"
       |}""".stripMargin
}.mkString("[", ",", "]")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM