我有这样的Map
:
val map: Map[String, Any] = Map(
"Item Version" -> 1.0,
"Item Creation Time" -> "2019-04-14 14:15:09",
"Trade Dictionary" -> Map(
"Country" -> "India",
"TradeNumber" -> "1",
"action" -> Map(
"Action1" -> false
),
"Value" -> "XXXXXXXXXXXXXXX"
),
"Payments" -> Map(
"Payment Details" -> List(
Map(
"Payment Date" -> "2019-04-11",
"Payment Type" -> "Rej"
))))
我写了一段代码:
def flattenMap(map: Map[String, Any]): Map[String, Any] = {
val c = map.flatten {
case ((key, map : Map[String, Any])) => map
case ((key, value)) => Map(key -> value)
// case ((key, List(map))) =>
}.toMap
return c
}
def secondFlatten(map: Map[String, Any]): Map[String, Any] = {
val c=map.flatten {
case ((key, map : Map[String, Any])) => flattenMap(map)
case ((key, value)) => Map(key -> value)
}.toMap
return c
}
哪个给出这样的输出:
(Country, India)
(Action1, false)
(Value, XXXXXXXXXXXXXXX)
(Item Version, 1.0)
(TradeNumber, 1)
(Item Creation Time, 2019-04-14 14:15:09)
(Payment Details, List(Map(Payment Date -> 2019-04-11, Payment Type -> Rej)))
我想进行一些代码更改,在其中我可以将地图列表转换为类似于输出的地图,即应该代替(Payment Details,List(Map(Payment Date -> 2019-04-11, Payment Type -> Rej)))
,我应该得到:
(Payment Date , 2019-04-11),
(Payment Type , Rej)
答案 0 :(得分:0)
对于您的特定情况,您可以更新flattenMap
方法:
def flattenMap(map: Map[String, Any]): Map[String, Any] = {
val c = map.flatten {
case ((key, map : Map[String, Any])) => map
case ((key, value)) => Map(key -> value)
case ((key, map :: Nil)) => map
}.toMap
return c
}
如果要处理多个地图的列表,则可能应该将返回的值包装到列表中,因此请使用某种flatMap
而不是toMap
。
答案 1 :(得分:0)
您应该处理地图列表。根据您提供的示例,我已将您的方法更新为:
def secondFlatten(map: Map[String, Any]):Map[String, Any]={
map.flatten {
case ((key, map : Map[String, Any])) => {
map.flatten {
case ((key: String, l: List[Map[String, Any]])) => l.head
case ((key: String, m : Map[String, Any])) => m
case (key: String, value: String) => Map(key -> value)
}
}
case ((key, value)) => Map(key -> value)
}.toMap
}
这可能对您在评论中提出的问题很有用。它将所有键附加在一起,并在地图中保留价值。如果您需要更复杂的解决方案,可以进一步扩展它:
def secondFlatten(map: Map[String, Any]):Map[String, Any]={
map.flatten {
case ((key, map : Map[String, Any])) => {
map.flatten {
case ((innerKey: String, l: List[Map[String, Any]])) => l.head.map{case (x:String, y: Any)=> (s"$key--$innerKey--$x"-> y)}
case ((innerKey: String, m : Map[String, Any])) => m.map{case (x:String, y: Any)=> (s"$key--$innerKey--$x"-> y)}
case (innerKey: String, value: String) => Map(s"$key--$innerKey"-> value)
}
}
case ((key, value)) => Map(key -> value)
}.toMap
}