簡體   English   中英

如何從Scala中的foreach讀取Map值

[英]How to read Map values from foreach in scala

// Reads Json file                           
val input_file = ("\\path\\to\\MyNew.json");

val json_content = scala.io.Source.fromFile(input_file).mkString
// parsing the json file
val details = JSON.parseFull(json_content)
// checking the matched result
details match {
    case mayBeList: Some[Map[String, Any]] =>
    val z = mayBeList.get.tails.toSet.flatten 
    z.foreach(println)
    case None => println("Parsing failed")
    case other => println("Unknown data structure: " + other)
}

得到以下輸出:

Map(Name -> Harish, Company -> In Equity, Sal -> 50000)
Map(Name -> Veer, Company -> InOut, Sal -> 20000)
Map(Name -> Zara, Company -> InWhich, Sal -> 90000)
Map(Name -> Singh, Company -> InWay, Sal -> 30000)
Map(Name -> Chandra, Company -> InSome, Sal -> 60000)

預期產量

Harish, In Quality, 50000- (only values of Map)

使用.values的價值觀和.keys的鑰匙。

val m: Map[String, Int] = Map("a" -> 1, "b" -> 2)

m.values // res0: Iterable[Int] = MapLike(1, 2)
m.keys // res1: Iterable[String] = Set(a, b)

您所需要做的就是遍歷list的元素(例如z並從每個map提取值,如下所示:

List(Map("Name" -> "Harish", "Company" -> "In Equity", "Sal" -> 50000),
  Map("Name" -> "Veer", "Company" -> "InOut", "Sal" -> 20000),
  Map("Name" -> "Zara", "Company" -> "InWhich", "Sal" -> 90000),
  Map("Name" -> "Singh", "Company" -> "InWay", "Sal" -> 30000),
  Map("Name" -> "Chandra", "Company" -> "InSome", "Sal" -> 60000)
)
.map(_.values.toList).foreach(println)
//List[List[Any]] = List(List(Harish, In Equity, 50000), List(Veer, InOut, 20000), List(Zara, InWhich, 90000), List(Singh, InWay, 30000), List(Chandra, InSome, 60000))

希望這對您有幫助。

更新

為了回應您的評論,請使用此代碼

import scala.util.parsing.json._

val input_file = ("C:\\Users\\VishalK\\IdeaProjects\\ScalaCassan\\src\\main\\scala\\MyNew.json");

val json_content = scala.io.Source.fromFile(input_file)
// parsing the json file

val details: Option[Any] = JSON.parseFull(json_content.mkString)

details match {
  case mayBeList: Some[Any] =>
    mayBeList.getOrElse(Seq.empty[Map[String, Any]]).asInstanceOf[List[Map[String, Any]]].map(_.values.toList).toSet
  case None => println("Parsing failed")
}

在您的比賽區中:

  • 在第一種情況下,我不明白為什么在Any數據類型上使用.tails.toSet.flatten
  • 您可以刪除第三種情況,因為“ Some和“ NoneOption數據類型的唯一可能結果。
scala> val l = List(Map("Name" -> "Harish", "Company" -> "In Equity", "Sal" -> 50000),
     |   Map("Name" -> "Veer", "Company" -> "InOut", "Sal" -> 20000),
     |   Map("Name" -> "Zara", "Company" -> "InWhich", "Sal" -> 90000),
     |   Map("Name" -> "Singh", "Company" -> "InWay", "Sal" -> 30000),
     |   Map("Name" -> "Chandra", "Company" -> "InSome", "Sal" -> 60000)
     | )
l: List[scala.collection.immutable.Map[String,Any]] = List(Map(Name -> Harish, Company -> In Equity, Sal -> 50000), Map(Name -> Veer, Company -> InOut, Sal -> 20000), Map(Name -> Zara, Company -> InWhich, Sal -> 90000), Map(Name -> Singh, Company -> InWay, Sal -> 30000), Map(Name -> Chandra, Company -> InSome, Sal -> 60000))

scala> l.map(_.values).foreach(x => println(x.toList.mkString(", ")))
Harish, In Equity, 50000
Veer, InOut, 20000
Zara, InWhich, 90000
Singh, InWay, 30000
Chandra, InSome, 60000

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM