Scala Play Json Reads

折月煮酒 提交于 2019-12-21 12:04:15

问题


I have a sample code as 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 get Json Validation Error when I try to parse the json into Scala object (Business Class in this case). The error is

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 my json is like

val jsonValue = """
    {
      "name":"Some Business Name",
      "preferredUrl":"someurl",
      "businessPhone":"somenumber",
      "retailer":
      {
        "firstName":"Some",
        "lastName":"One",
        "email":"someone@somewhere.com",
        "mobileNo":"someothernumber",
        "password":"$^^HFKH*"
      }
  }
  """

Note that the outer bracket "{" and the "business:" key are removed. I get a JsSuccess. How do I write the reads for the Json as in the first case? Also, how can I do it in a generic way?

Please help.


回答1:


Just add the business key in the path:

 implicit val bsnsRds = (
      (__ \ "business" \ "name").read[String] ~
      (__ \ "business" \ "preferredUrl").read[String] ~
      (__ \ "business" \ "businessPhone").read[String] ~
      (__ \ "business" \ "retailer").read[Retailer](rltRds)
    )(Business)



回答2:


Slight variation to the above:

implicit val bsnsRds = ({
  val business = (__ \ "business")
  (business \ "name").read[String] ~
  (business \ "preferredUrl").read[String] ~
  (business \ "businessPhone").read[String] ~
  (business \ "retailer").read[Retailer](rltRds)
})(Business)


来源:https://stackoverflow.com/questions/18122175/scala-play-json-reads

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!