8

我有一个示例代码如下。

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
  }

}

当我尝试将 json 解析为 Scala 对象(在本例中为业务类)时,出现 Json 验证错误。错误是

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())))))

但是,如果我的 json 像

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

请注意,外括号“{”和“business:”键已被删除。我得到了一个 JsSuccess。在第一种情况下,我如何为 Json 编写读取?另外,我怎样才能以通用的方式做到这一点?

请帮忙。

4

2 回答 2

8

只需business在路径中添加密钥:

 implicit val bsnsRds = (
      (__ \ "business" \ "name").read[String] ~
      (__ \ "business" \ "preferredUrl").read[String] ~
      (__ \ "business" \ "businessPhone").read[String] ~
      (__ \ "business" \ "retailer").read[Retailer](rltRds)
    )(Business)
于 2013-08-08T09:26:52.520 回答
2

与上述略有不同:

implicit val bsnsRds = ({
  val business = (__ \ "business")
  (business \ "name").read[String] ~
  (business \ "preferredUrl").read[String] ~
  (business \ "businessPhone").read[String] ~
  (business \ "retailer").read[Retailer](rltRds)
})(Business)
于 2015-12-04T00:51:06.490 回答