Parsing Json string in Scala using play mode
I started trying Scala and Play to parse through Json data, and the following tutorial is at https://www.playframework.com/documentation/2.3.9/ScalaJson . Now when I try to run the example code given here, which is:
val json: JsValue = Json.parse("""{
"name" : "Watership Down",
"location" : {
"lat" : 51.235685,
"long" : -1.309197
},
"residents" : [ {
"name" : "Fiver",
"age" : 4,
"role" : null
}, {
"name" : "Bigwig",
"age" : 6,
"role" : "Owsla"
} ]
}
""")
val lat = json \ "location" \ "lat"
I am getting the following error:
java.lang.NoSuchMethodError: play.api.libs.json.JsValue.$bslash(Ljava/lang/String;)Lplay/api/libs/json/JsValue;
What am I doing wrong? I am using Scala 2.10 and Play 2.3.9.
Thank.
+3
source to share
1 answer
In Play 2.4.x, JsLookupResult represents a value on a specific Json path, either the actual Json node or undefined. JsLookupResult has two subclasses, JsDefined and JsUndefined respectively.
You can change your code like this:
val name: JsLookupResult = json \ "user" \ "name"
name match {
case JsDefined(v) => println(s"name = ${v.toString}")
case undefined: JsUndefined => println(undefined.validationError)
}
+8
source to share