如何在收到的 json 字段中转义 \
how to escape \ in received json fields
我的应用程序在 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(JsReadable.scala:25)
问题 1- 转换失败是否是因为接收到的消息中的 json
字段包含 \
?
问题 2- 在 "method under test" 中,我没有在响应中明确添加 \
。发送响应时,我只是在 case class
上调用 toString()
。 Json.toJson(externalProfileAPI).toString()
。如果 \
是问题,我怎样才能不在发送的响应中发送它们或在客户端转义它们?
Question 1: Is the conversion failing because the json fields in the
received message has \
in it?
是的,因为当前状态下的字符串不正确Json,您必须删除(转义)那些反斜杠。
Question 2: If \
is the issue, how can I either not send them in the
sent response or escape them at the client side?
您有没有在发送端的 json 上调用 JSON.stringify()
?如果是这样,删除它将删除反斜杠。否则可以在接收端调用JSON.parse(YOUR_STRING)
转义反斜杠
我的应用程序在 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(JsReadable.scala:25)
问题 1- 转换失败是否是因为接收到的消息中的 json
字段包含 \
?
问题 2- 在 "method under test" 中,我没有在响应中明确添加 \
。发送响应时,我只是在 case class
上调用 toString()
。 Json.toJson(externalProfileAPI).toString()
。如果 \
是问题,我怎样才能不在发送的响应中发送它们或在客户端转义它们?
Question 1: Is the conversion failing because the json fields in the received message has
\
in it?
是的,因为当前状态下的字符串不正确Json,您必须删除(转义)那些反斜杠。
Question 2: If
\
is the issue, how can I either not send them in the sent response or escape them at the client side?
您有没有在发送端的 json 上调用 JSON.stringify()
?如果是这样,删除它将删除反斜杠。否则可以在接收端调用JSON.parse(YOUR_STRING)
转义反斜杠