如何从Scala中的foreach读取Map值

时间:2018-12-18 12:58:35

标签: json scala

let result = await OgShift.OgShift.find({
        ShiftStartTime: {
             $gte:new Date("2018-01-12"),
        },
        ShiftEndTime: {
            $lt: new Date("2019-12-31")
        }
    })

获得以下输出:

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

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

您需要做的就是遍历func observe(type: String) -> Observable<[MyRealmObject]> { let observableCalculation = { let realm = configureRealm() // 1 let predicate = NSPredicate(format: "type = %@", type) let resources = realm .objects(MyRealmObject.self) .filter(predicate) return Observable.array(from: resources) // 2 } if Thread.current == self.realmObservationThread { return observableCalculation() } var o: Observable<[MyRealmObject]>! let g = DispatchGroup() g.enter() self.realmObservationThread.runloop?.perform { o = observableCalculation() g.leave() } g.wait() return o } 的元素,即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") } 数据类型上使用.tails.toSet.flatten
  • 您可以删除第三种情况,因为Any数据类型的唯一可能结果是SomeNone

答案 2 :(得分:0)

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