Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading JsValue in Scala

How do I access the name of the first resident? Here is the Json file. What comes after the following?

val bigwig = (json \ "residents")(1)..... 


import play.api.libs.json._

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"
  } ]
}
""")
like image 952
ruff Avatar asked May 02 '16 23:05

ruff


People also ask

What is a JSValue?

You use the JSValue class to convert basic values, such as numbers and strings, between JavaScript and Objective-C or Swift representations to pass data between native code and JavaScript code.

What is play JSON?

The Play JSON API provides implicit Writes for most basic types, such as Int , Double , String , and Boolean . It also supports Writes for collections of any type T that a Writes[T] exists. import play. json.


1 Answers

Quick and dirty (with no validation):

((jsonObject \ "residents").as[Seq[JsObject]].head \ "name").as[String]
like image 111
James Ward Avatar answered Oct 14 '22 11:10

James Ward