Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 如何在scala中从foreach读取映射值_Json_Scala - Fatal编程技术网

Json 如何在scala中从foreach读取映射值

Json 如何在scala中从foreach读取映射值,json,scala,Json,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 m

获取以下输出:

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

您所需要的就是遍历
列表中的元素,即
z
,然后像这样从每个
映射中提取值

Harish, In Quality, 50000- (only values of 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))
在你的比赛区:

  • 在第一种情况下,我不明白为什么要在
    任何
    数据类型上使用
    .tails.toSet.flatte
  • 您可以删除第三种情况,因为
    Some
    None
    选项
    数据类型的唯一可能结果

使用
.values
作为值,使用
.keys
作为键

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

你能提供你用作输入的文件的内容吗?
Map
has
。value
method[{“Name”:“Harish”,“Company”:“In Equity”,“Sal”:“50000”},{“Name”:“Chandra”,“Company”:“InSome”,“Sal”:“60000”},{“Name”:“Singh”,“Company”:“InWay”,“Sal”:“30000”{“Name”:“30000”{“Name”:“Veer”,“Company”:“InOut”,“Sal”:“20000”},{“Name”:“Zara”,“Company”:“InWhich”,“Sal”:“90000”}]显然,您可以使用相关的
方法将它们转换为列表或Seq或任何内容(例如
.toList
toSeq
等).Hi James谢谢你的回复,但它给我带来了一个错误。更新了答案。详细信息匹配{case mayBeList:Some[Map[String,Any]=>val z=mayBeList.get.tails.toList.flatte z.Map(.values)。foreach(x=>println(x.toList.mkString(,“”))case None=>println(“解析失败”)case other=>println(“”)未知的数据结构:“+other”)}引发以下错误:。错误:(26,14)值不是(String,Any)z.map(.values)的成员。foreach(x=>println(x.toList.mkString(“,”))上述解决方案不是类型安全的。它仅适用于
[{”Name:“Harish”,“Company:“in Equity”,“Sal:”格式的数据50000“},{”Name:“Veer”,“Company:“InOut”,“Sal:“20000”}]
。您还可以展示一下您提供的JSON结构吗?您还可以告诉我为什么在
任何
数据类型上使用
mayBeList.get.tails.toSet.flatte
?JSON::[{”Name:“Harish”,“Company:“In Equity”,“Sal:“50000”},{”Name:“Chandra”,“公司“:“InSome”,“Sal”:“60000”},{“Name”:“Singh”,“Company”:“InWay”,“Sal”:“30000”},{“Name”:“Veer”,“Company”:“InOut”,“Sal”:“20000”},{“Name”:“Zara”,“Company”:“InWhere”,“Sal”:“90000”}]我使用“toSet.Flatte”,因为它给我带来了独特的结果,就像我使用“toList”一样“然后它会给我重复的记录。嗨,airudah,谢谢你的回复,但这里你是在列表(Map)中硬编码键和值对),但我编写的程序正在动态读取json文件,我需要单独的记录值。
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)
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