【发布时间】:2019-04-03 05:47:34
【问题描述】:
我的应用程序在 Http 响应中收到 json
{"result":"success","additional-info":"{\"external-profile\":{\"email\":\"test@test.com\",\"firstname\":\"ln\",\"lastname\":\"fn\",\"password\":\"somePassword\"}}"}
我写了一个reads 来将传入的消息转换成case class
implicit val externalProfileAPIReads:Reads[ExternalUserProfileAPI] = (
(JsPath \ "external-profile").read[ExternalUserProfile]
).map((x:ExternalUserProfile)=>(ExternalUserProfileAPI.apply(x)))
但在我的单元测试中,当我尝试转换消息时,转换失败。
val message = (responseBody \ "additional-info").get.as[ExternalUserProfileAPI]
错误是
JsResultException(errors:List((/external-profile,List(JsonValidationError(List(error.path.missing),WrappedArray())))))
play.api.libs.json.JsResultException: JsResultException(errors:List((/external-profile,List(JsonValidationError(List(error.path.missing),WrappedArray())))))
at play.api.libs.json.JsReadable.$anonfun$as$2(JsReadable.scala:25)
问题 1- 是否因为接收到的消息中的 json 字段中包含 \ 而导致转换失败?
问题 2- 在“被测方法”中,我没有在响应中明确添加 \s。我只是在发送响应时在case class 上调用toString()。 Json.toJson(externalProfileAPI).toString()。如果\ 是问题所在,我怎样才能不在发送的响应中发送它们或在客户端转义它们?
【问题讨论】: