繁体   English   中英

Play2 Scala:将 Json 反序列化为对象列表

[英]Play2 Scala: Deserialize Json into a List of objects

我正在尝试将使用 Play 的WSClient获得的 json 正文响应反序列化为对象列表,我认为我离成功还不算太近,但可能还有最后一块拼图缺少我。

这些是传入 json 必须反序列化为的 case 类(及其伴随对象)。

EmployerStoreDTO:

import play.api.libs.json._

case class EmployerStoreDTO (id: String, storeName: String)

object EmployerStoreDTO {
  implicit val reads: Reads[EmployerStoreDTO] = Json.reads[EmployerStoreDTO]

  implicit val employerStoreDTOlistReads: Reads[List[EmployerStoreDTO]] = Reads.list[EmployerStoreDTO]
}

公司雇主DTO:

import java.time.DayOfWeek

import play.api.libs.json._
import play.api.libs.json.Reads._
import play.api.libs.functional.syntax._

case class CompanyEmployerDTO(id: String,
                              companyName: String,
                              companyUrl: Option[String],
                              description: Option[String],
                              startDayOfWeek: DayOfWeek,
                              logoUrl: Option[String],
                              stores: List[EmployerStoreDTO] = List(),
                              supportHelpText: Option[String],
                              themeId: Option[String]) {
}

object CompanyEmployerDTO {

  import model.EmployerStoreDTO._

  implicit val startDayOfWeekReads: Reads[DayOfWeek] = (JsPath \ "weekStartDay").read[Int].map(DayOfWeek.of)

  implicit val reads: Reads[CompanyEmployerDTO] = (
    (JsPath \ "id").read[String] and
      (JsPath \ "companyName").read[String] and
      (JsPath \ "companyUrl").readNullable[String] and
      (JsPath \ "description").readNullable[String] and startDayOfWeekReads and
      (JsPath \ "logoUrl").readNullable[String] and employerStoreDTOlistReads and
      (JsPath \ "supportHelpText").readNullable[String] and
      (JsPath \ "themeId").readNullable[String]
    ) (CompanyEmployerDTO.apply _)

  implicit val companyEmployerDTOListReads: Reads[List[CompanyEmployerDTO]] = Reads.list[CompanyEmployerDTO]
}

CompanyEmployerCollectionDTO:

import play.api.libs.json.Reads._
import play.api.libs.json._

case class CompanyEmployerCollectionDTO (companies: List[CompanyEmployerDTO])

object CompanyEmployerCollectionDTO {

  implicit val reads: Reads[CompanyEmployerCollectionDTO] =
    (JsPath \ "companies").read[List[CompanyEmployerDTO]].map(CompanyEmployerCollectionDTO.apply)

}

我看到我的WsClient收到了有效负载: 在此处输入图片说明

但当我试图反序列化响应如下: response.json.as[CompanyEmployerCollectionDTO]

我收到此错误:

Caused by: play.api.libs.json.JsResultException: JsResultException(errors:List((/companies(0),List(JsonValidationError(List(error.expected.jsarray),WrappedArray())))))
    at play.api.libs.json.JsReadable.$anonfun$as$2(JsReadable.scala:25)
    at play.api.libs.json.JsError.fold(JsResult.scala:64)
    at play.api.libs.json.JsReadable.as(JsReadable.scala:24)
    at play.api.libs.json.JsReadable.as$(JsReadable.scala:23)
    at play.api.libs.json.JsObject.as(JsValue.scala:124)
    at services.com.mycompany.ApiEmployeeClient.$anonfun$callGetEmployers$2(ApiEmployeeClient.scala:33)
    at scala.util.Success.$anonfun$map$1(Try.scala:255)
    at scala.util.Success.map(Try.scala:213)
    at scala.concurrent.Future.$anonfun$map$1(Future.scala:292)
    at scala.concurrent.impl.Promise.liftedTree1$1(Promise.scala:33)

问题是裸露and employerStoreDTOlistReads 由于您尚未指定路径(对于其他CompanyEmployerDTO成员),解码器将尝试将当前 JSON 值本身(代表CompanyEmployerDTO的对象)解码为EmployerStoreDTO列表。

and (JsPath \\ "stores").read[List[EmployerStoreDTO]]替换这两个词应该可以修复您的代码,但是对于值得的,您还可以通过废弃List实例(将提供)来大大简化您的实现自动),删除导入等:

import play.api.libs.json._

case class EmployerStoreDTO (id: String, storeName: String)

object EmployerStoreDTO {
  implicit val reads: Reads[EmployerStoreDTO] = Json.reads[EmployerStoreDTO]
}

import java.time.DayOfWeek

import play.api.libs.json._
import play.api.libs.json.Reads._
import play.api.libs.functional.syntax._

case class CompanyEmployerDTO(id: String,
                              companyName: String,
                              companyUrl: Option[String],
                              description: Option[String],
                              startDayOfWeek: DayOfWeek,
                              logoUrl: Option[String],
                              stores: List[EmployerStoreDTO] = List(),
                              supportHelpText: Option[String],
                              themeId: Option[String]) {
}

object CompanyEmployerDTO {
  implicit val reads: Reads[CompanyEmployerDTO] = (
    (JsPath \ "id").read[String] and
      (JsPath \ "companyName").read[String] and
      (JsPath \ "companyUrl").readNullable[String] and
      (JsPath \ "description").readNullable[String] and
      (JsPath \ "weekStartDay").read[Int].map(DayOfWeek.of) and
      (JsPath \ "logoUrl").readNullable[String] and
      (JsPath \ "stores").read[List[EmployerStoreDTO]] and
      (JsPath \ "supportHelpText").readNullable[String] and
      (JsPath \ "themeId").readNullable[String]
    ) (CompanyEmployerDTO.apply _)
}

import play.api.libs.json.Reads._
import play.api.libs.json._

case class CompanyEmployerCollectionDTO (companies: List[CompanyEmployerDTO])

object CompanyEmployerCollectionDTO {
  implicit val reads: Reads[CompanyEmployerCollectionDTO] =
    (JsPath \ "companies").read[List[CompanyEmployerDTO]].map(CompanyEmployerCollectionDTO.apply)
}

这将与您的代码做完全相同的事情,但更简洁和易于管理。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM