简体   繁体   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)
}

getting following Output: 得到以下输出:

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)

Expected Output 预期产量

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

Use .values for the values and .keys for the keys. 使用.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)

All you need is to iterate though the elements of your list ie z and extract values from each map like this, 您所需要做的就是遍历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))

Hope this helps you. 希望这对您有帮助。

Update 更新

In response to your comment, use this code 为了回应您的评论,请使用此代码

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

in your match block : 在您的比赛区中:

  • In first case I don't get why you are using .tails.toSet.flatten on Any data type. 在第一种情况下,我不明白为什么在Any数据类型上使用.tails.toSet.flatten
  • You can remove the third case as Some and None are the only possible outcomes of Option data-type. 您可以删除第三种情况,因为“ 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