JsonValidationError 不是对象错误解释
JsonValidationError is not an object error interpretation
有没有人知道这个错误的原因可能是什么?
Future(
Success(
JsError(
List(
(,List(
JsonValidationError(
List(
[{"FIELD1":"field1value","FIELD2":"field2value","FIELD3":"field3value"}] is not an object
),
ArraySeq())
))))))
谢谢!
UPDATE 生成此输出的代码如下所示:
val queryResponse: Future[JsResult[MyObject]] = ws.url(queryURL)
.withCookies(cookies.toSeq :_*)
.get().map{response =>
(response.json \ "object").validate[MyObject]
}
来自服务器的响应似乎是 JSON 个对象的列表。
MyObject 结构如下:
case class MyObject(
FIELD1: Int,
FIELD2: Int,
FIELD3: String,
FIELD4: String,
FIELD5: String,
FIELD6: Int,
FIELD7: String,
FIELD8: String,
FIELD9: Option[String],
FIELD10: String,
FIELD11: String,
FIELD12: String,
FIELD13: Int
)
object MyObject
{
implicit val format: Format[MyObject] = Json.format
implicit val myObjectWrites = Json.writes[MyObject]
implicit val myObjectReads: Reads[MyObject] = (
(JsPath \ "FIELD1").read[Int] and
(JsPath \ "FIELD2").read[Int] and
(JsPath \ "FIELD3").read[String] and
(JsPath \ "FIELD4").read[String] and
(JsPath \ "FIELD5").read[String] and
(JsPath \ "FIELD6").read[Int] and
(JsPath \ "FIELD7").read[String] and
(JsPath \ "FIELD8").read[String] and
(JsPath \ "FIELD9").readNullable[String] and
(JsPath \ "FIELD10").read[String] and
(JsPath \ "FIELD11").read[String] and
(JsPath \ "FIELD12").read[String] and
(JsPath \ "FIELD13").read[Int])(MyObject.apply _)
}
您试图从 JSON 文档中解析对象,该文档如下所示:
{
"object": [
{ "FIELD1":"field1value","FIELD2":"field2value","FIELD3":"field3value"}
]
}
改为使用
(response.json \ "object" \ 0).validate[MyObject]
或
(response.json \ "object").validate[Seq[MyObject]]
这里有一个 scastie 片段来说明:https://scastie.scala-lang.org/avdv/gNc2SFkIR2ukvDtr7ZiYJw
有没有人知道这个错误的原因可能是什么?
Future(
Success(
JsError(
List(
(,List(
JsonValidationError(
List(
[{"FIELD1":"field1value","FIELD2":"field2value","FIELD3":"field3value"}] is not an object
),
ArraySeq())
))))))
谢谢!
UPDATE 生成此输出的代码如下所示:
val queryResponse: Future[JsResult[MyObject]] = ws.url(queryURL)
.withCookies(cookies.toSeq :_*)
.get().map{response =>
(response.json \ "object").validate[MyObject]
}
来自服务器的响应似乎是 JSON 个对象的列表。
MyObject 结构如下:
case class MyObject(
FIELD1: Int,
FIELD2: Int,
FIELD3: String,
FIELD4: String,
FIELD5: String,
FIELD6: Int,
FIELD7: String,
FIELD8: String,
FIELD9: Option[String],
FIELD10: String,
FIELD11: String,
FIELD12: String,
FIELD13: Int
)
object MyObject
{
implicit val format: Format[MyObject] = Json.format
implicit val myObjectWrites = Json.writes[MyObject]
implicit val myObjectReads: Reads[MyObject] = (
(JsPath \ "FIELD1").read[Int] and
(JsPath \ "FIELD2").read[Int] and
(JsPath \ "FIELD3").read[String] and
(JsPath \ "FIELD4").read[String] and
(JsPath \ "FIELD5").read[String] and
(JsPath \ "FIELD6").read[Int] and
(JsPath \ "FIELD7").read[String] and
(JsPath \ "FIELD8").read[String] and
(JsPath \ "FIELD9").readNullable[String] and
(JsPath \ "FIELD10").read[String] and
(JsPath \ "FIELD11").read[String] and
(JsPath \ "FIELD12").read[String] and
(JsPath \ "FIELD13").read[Int])(MyObject.apply _)
}
您试图从 JSON 文档中解析对象,该文档如下所示:
{
"object": [
{ "FIELD1":"field1value","FIELD2":"field2value","FIELD3":"field3value"}
]
}
改为使用
(response.json \ "object" \ 0).validate[MyObject]
或
(response.json \ "object").validate[Seq[MyObject]]
这里有一个 scastie 片段来说明:https://scastie.scala-lang.org/avdv/gNc2SFkIR2ukvDtr7ZiYJw