Scala Play Json阅读 [英] Scala Play Json Reads
问题描述
我有以下示例代码.
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对象(在这种情况下为Business Class)时,出现Json验证错误.错误是
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())))))
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是
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*"
}
}
"""
请注意,删除了外部括号"{"和"business:"键. 我得到一个JsSuccess.与第一种情况一样,我该如何写Json的读物? 另外,我该如何通用?
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?
请帮助.
推荐答案
只需在路径中添加business
键:
implicit val bsnsRds = (
(__ \ "business" \ "name").read[String] ~
(__ \ "business" \ "preferredUrl").read[String] ~
(__ \ "business" \ "businessPhone").read[String] ~
(__ \ "business" \ "retailer").read[Retailer](rltRds)
)(Business)
这篇关于Scala Play Json阅读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!