检查play.api.libs.json.Json中是否存在密钥

时间:2014-07-23 07:35:15

标签: json scala collections playframework scala-collections

contains类似于play.api.libs.json.Json

的功能
val data=Map("id" -> "240190", "password" -> "password","email" -> "email@domain.com")

data.contains("email")//true


val info=Json.obj("id" -> "240190", "password" -> "password","email" -> "email@domain.com")

现在如何检查info是否包含email

3 个答案:

答案 0 :(得分:21)

info.keys.contains("email")

.keys会为您提供一个包含键值的Set,然后您可以调用contains方法,我不确定是否有更直接的方法这样做。

答案 1 :(得分:6)

(info \ "email").asOpt[String].isEmpty

asOpt将返回Optional,我们可以进行isEmpty简单检查,这样做就可以了。

答案 2 :(得分:5)

(info \ "email").asOpt[String] match {
  case Some(data) => println("here is the value for the key email represented by variable data" + data)
  case None => println("email key is not found") 
}