Scala Play Json Reads -


i have sample code below.

import play.api.libs.json._ import play.api.libs.functional.syntax._ import play.api.data.validation.validationerror import play.api.libs.json.reads._  case class retailer(firstname:string,lastname:string,email:string,mobileno:string,password:string) case class business(name:string,preferredurl:string,businessphone:string,retailer:retailer)  object jsontest {   val jsonvalue = """   {     "business":     {       "name":"some business name",       "preferredurl":"someurl",       "businessphone":"somenumber",       "retailer":       {         "firstname":"some",         "lastname":"one",         "email":"someone@somewhere.com",         "mobileno":"someothernumber",         "password":"$^^hfkh*"       }     }    }   """   def printjson ={      implicit val rltrds = (       (__ \ "firstname").read[string] ~       (__ \ "lastname").read[string] ~       (__ \ "email").read[string] ~       (__ \ "mobileno").read[string] ~       (__ \ "password").read[string]     )(retailer)          implicit val bsnsrds = (       (__ \ "name").read[string] ~       (__ \ "preferredurl").read[string] ~       (__ \ "businessphone").read[string] ~       (__ \ "retailer").read[retailer](rltrds)     )(business)           val buisness = json.parse(jsonvalue).validate[business](bsnsrds)     val bus = new business("some business","somebusinessurl","somenumber", new retailer("some","one","someone@somewhere.com","someothernumber","$^^hfkh*"))     //val json = json.tojson(bus)      println(buisness)   }          def main(args: array[string]): unit = {     printjson   }  } 

i json validation error when try parse json scala object (business class in case). error

jserror(list((/retailer,list(validationerror(validate.error.missing-path,wrappedarray()))), (/preferredurl,list(validationerror(validate.error.missing-path,wrappedarray()))), (/name,list(validationerror(validate.error.missing-path,wrappedarray()))), (/businessphone,list(validationerror(validate.error.missing-path,wrappedarray())))))

however if json

val jsonvalue = """     {       "name":"some business name",       "preferredurl":"someurl",       "businessphone":"somenumber",       "retailer":       {         "firstname":"some",         "lastname":"one",         "email":"someone@somewhere.com",         "mobileno":"someothernumber",         "password":"$^^hfkh*"       }   }   """ 

note outer bracket "{" , "business:" key removed. jssuccess. how write reads json in first case? also, how can in generic way?

please help.

just add business key in path:

 implicit val bsnsrds = (       (__ \ "business" \ "name").read[string] ~       (__ \ "business" \ "preferredurl").read[string] ~       (__ \ "business" \ "businessphone").read[string] ~       (__ \ "business" \ "retailer").read[retailer](rltrds)     )(business) 

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -