使用 scala.util.parsing.json 在 Scala 中解析 Json
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21214561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Json parsing in Scala with scala.util.parsing.json
提问by OZKA
I have a json object "{"id":1,"name":"OZKA","birthDate":"1981-02-08T20:00:00.000Z","monthRevenue":1000.75,"developer":true}"and the code:
我有一个 json 对象"{"id":1,"name":"OZKA","birthDate":"1981-02-08T20:00:00.000Z","monthRevenue":1000.75,"developer":true}"和代码:
println(request.getParameter("content"))//{"id":1,"name":"OZKA","birthDate":"1981-02-08T20:00:00.000Z","monthRevenue":1000.75,"developer":true}
val result = scala.util.parsing.json.JSON.parseFull(request.getParameter("content"))
result match {
case Some(e) => { println(e); //output: Map(name -> OZKA, monthRevenue -> 1000.75, developer -> true, birthDate -> 1981-02-08T20:00:00.000Z, id -> 1.0)
e.foreach((key: Any, value: Any) => {println(key + ":" + value)})
}
case None => println("Failed.")
}
, when I try to call map or foreach function, compiler throws an error "value foreach is not a member of Any". Can anybody suggest me a way, how i can parse this json string and convert its to Scala types
,当我尝试调用 map 或 foreach 函数时,编译器抛出错误“值 foreach 不是 Any 的成员”。任何人都可以给我建议一种方法,我如何解析这个 json 字符串并将其转换为 Scala 类型
回答by z33m
The error that you get is caused because the compiler has no way of knowing the type of ein Some(e)pattern, its infered as Any. And Anydoesnt have a foreachmethod. You can solve this by explicitly specifying the type of eas a Map.
你得到的错误是因为编译器无法知道einSome(e)模式的类型,它被推断为Any. 并且Any没有foreach方法。您可以通过将 的类型显式指定e为 a来解决此问题Map。
Secondly, for a Map foreachhas the signature foreach(f: ((A, B)) ? Unit): Unit. The argument of the anonymous function is a tuple containing the key and value.
其次,对于 Mapforeach具有签名foreach(f: ((A, B)) ? Unit): Unit。匿名函数的参数是一个包含键和值的元组。
Try something like this:
尝试这样的事情:
println(request.getParameter("content"))//{"id":1,"name":"OZKA","birthDate":"1981-02-08T20:00:00.000Z","monthRevenue":1000.75,"developer":true}
val result = scala.util.parsing.json.JSON.parseFull(request.getParameter("content"))
result match {
case Some(e:Map[String,String]) => {
println(e); //output: Map(name -> OZKA, monthRevenue -> 1000.75, developer -> true, birthDate -> 1981-02-08T20:00:00.000Z, id -> 1.0)
e.foreach { pair =>
println(pair._1 + ":" + pair._2)
}
}
case None => println("Failed.")
}
回答by Patel Sunil
you may access keys and values in any json as:
您可以访问任何 json 中的键和值,如下所示:
import scala.util.parsing.json.JSON
import scala.collection.immutable.Map
val jsonMap = JSON.parseFull(response).getOrElse(0).asInstanceOf[Map[String,String]]
val innerMap = jsonMap("result").asInstanceOf[Map[String,String]]
innerMap.keys //will give keys
innerMap("anykey") //will give value for any key anykey

