Reading value from JSON string Scala using ScalaJson -
i have json response follow
{ ......, "phonenumbers":[ { "info":[ ], "phonenumber":"1234567890", "phonenumbertype":"home_number" }, { "info":[ ], "phonenumber":"9876543210", "phonenumbertype":"work_number" }, { "info":[ ], "phonenumber":"7418529630", "phonenumbertype":"mobile_number" } ], ....... }
i need extract mobile number , try this
val jsonbody = json.parse(phonelistresponse.body) val phonelist: seq[jsvalue] = (jsonbody \\ "phonenumbers") val mobilenodes: seq[jsvalue] = phonelist.filter(number => { (number \\ "phonenumbertype").head.asinstanceof[jsstring].value == "mobile_number" }) (mobilenodes.head \\ "phonenumber").head.asinstanceof[jsstring].value
but mobilenodes empty. suggestion?
if using play-json
val phonelist: seq[string] = ((jsonbody \\ "phonenumbers")(0).as[list[jsvalue]]) .filter(x => (x \ "phonenumbertype").as[string] == "mobile_number") .map(x => (x \ "phonenumber").as[string])
repl output
scala> ((json \\ "phonenumbers")(0).as[list[jsvalue]]).foreach(println) {"info":[],"phonenumber":"1234567890","phonenumbertype":"home_number"} {"info":[],"phonenumber":"9876543210","phonenumbertype":"work_number"} {"info":[],"phonenumber":"7418529630","phonenumbertype":"mobile_number"} scala> ((json \\ "phonenumbers")(0).as[list[jsvalue]]).filter(x => (x \ "phonenumbertype").as[string] == "mobile_number") res31: list[play.api.libs.json.jsvalue] = list({"info":[],"phonenumber":"7418529630","phonenumbertype":"mobile_number"}) scala> ((json \\ "phonenumbers")(0).as[list[jsvalue]]).filter(x => (x \ "phonenumbertype").as[string] == "mobile_number").map(x => (x \ "phonenumber").as[string]) res32: list[string] = list(7418529630)
Comments
Post a Comment