JsResultException - unable to successfully parse json - 2.4x Play Scala -
i have following case class;
import java.sql.timestamp case class dose (date: timestamp, pthospitalnumber: string)
and in controller following code handle json;
import play.api.libs.json._ import play.api.libs.functional.syntax._ implicit val dosereads: reads[dose] = ( (jspath \ "date").read[long].map(long => new timestamp(long)) , (jspath \ "pthospitalnumber").read[string] )(dose.apply _) def adddoses() = action(bodyparsers.parse.json) { implicit request => val doses = (request.body \ "doses" ).as[list[dose]] //then iterate through list , return json response }
however, when json sent (from android device list[dose] using org.springframework.http.converter.json.mappingjackson2httpmessageconverter), keep getting error;
play.api.libs.json.jsresultexception: jsresultexception(errors:list((,list(validationerror(list([{"date":1445789736831, "hospitalnumber":"a059es21"},{"date":1445790530290,"hospitalnumber":"a059es21"}] not object),wrappedarray())))))
i tried following returned null;
val dosesjsresult = (request.body \ "doses" ).validate[list[dose]] val doses = dosesjsresult match{ case s: jssuccess[list[dose]] => s.get case e: jserror => null }
i can't figure out i'm doing wrong. can please?
the problem was trying find list called "doses" in json, didn't exist. needed parse request body list of dose;
implicit val dosedatareads = new reads[dose] { def reads(json: jsvalue): jsresult[dose] = { val date = (json \ "date").as[long] val hospitalnumber = (json \ "hospitalnumber").as[string] jssuccess(dose(hospitalnumber, new timestamp(date))) } } def adddoses() = action(bodyparsers.parse.json) { implicit request => val doses = request.body.as[list[dose]] //rest of code persist doses etc }
Comments
Post a Comment