将作为字节数组的结果主体反序列化为 json 响应

Deserialize body of result which is a byte array to json response

我正在尝试获取结果的主体并将其反序列化为 JSON。

   val str = """ {"protectedBitcoinAddress":"moLM1zdPBSh81eykhfcoLZ3myqUSy9FRRG"} """
   val request : JsValue = Json.parse(str)
   println("Request: " + request.toString)
   val result : Future[Result] = createUnsignedNLockTime()(FakeRequest().withJsonBody(request))

   whenReady(result, timeout(19 seconds), interval(5 millis)) { res =>
    println("==============================")
    val e: play.api.libs.iteratee.Enumerator[Array[Byte]] = res.body

    val bodyAsStr: scala.concurrent.Future[String] = e.run(play.api.libs.iteratee.Iteratee.fold("") { (memo, next) => memo + next })

    println("Body as string: " + Await.result(bodyAsStr,5 seconds))
    val bodyAsJsValue: scala.concurrent.Future[play.api.libs.json.JsValue] = e.map(play.api.libs.json.Json.parse)

    println("body as js: " + Await.result(bodyAsJsValue, 5 seconds))
    println("==============================")
  }

如何获取 Enumerator[scala.Array[scala.Byte]] 类型的内容并将其转换为 Playframework 中的 Json 对象?

这是你想要的吗?

 [info] Starting scala interpreter...
[info]
Welcome to Scala version 2.10.4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_79).
Type in expressions to have them evaluated.
Type :help for more information.

scala>import play.api.mvc.{Result, Results}
import play.api.mvc.{Result, Results}

scala>import play.api.libs.iteratee.{Enumerator, Iteratee}
import play.api.libs.iteratee.{Enumerator, Iteratee}

scala>import play.api.libs.json.Json
import play.api.libs.json.Json

scala>import scala.concurrent.{Await, Future}
import scala.concurrent.{Await, Future}

scala>import scala.concurrent.duration._
import scala.concurrent.duration._

scala>import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.ExecutionContext.Implicits.global

scala>val result: Result = Results.Ok(Json.stringify(Json.toJson(Map("k" -> "v")))).as("application/json")
result: play.api.mvc.Result = Result(200, Map(Content-Type -> application/json))

scala>val enumerator: Enumerator[Array[Byte]] = result.body
enumerator: play.api.libs.iteratee.Enumerator[Array[Byte]] = play.api.libs.iteratee.Enumerator$$anon@70d59654

scala>val bodyAsBytesEventually: Future[Array[Byte]] = enumerator.run(Iteratee.fold(Array.empty[Byte]) { (memo, nextChunk) => memo ++ nextChunk })
bodyAsBytesEventually: scala.concurrent.Future[Array[Byte]] = scala.concurrent.impl.Promise$DefaultPromise@8786f46

scala>val bodyAsStrEventually = bodyAsBytesEventually.map(bytes => new String(bytes))
bodyAsStrEventually: scala.concurrent.Future[String] = scala.concurrent.impl.Promise$DefaultPromise@442b7dd6

scala>val bodyAsStr = Await.result(bodyAsStrEventually, 1.seconds)
bodyAsStr: String = {"k":"v"} // In general, do not block unless you know what you are doing.

我认为您需要 Helpers.scala 将您的字节数组类型的响应转换为 string/json。

参考:this and this

您可以将代码修改为:

    val str = """ {"protectedBitcoinAddress":"moLM1zdPBSh81eykhfcoLZ3myqUSy9FRRG"} """
    val request : JsValue = Json.parse(str)
    println("Request: " + request.toString)
    val result : Future[Result] = createUnsignedNLockTime()(FakeRequest().withJsonBody(request))

    whenReady(result, timeout(19 seconds), interval(5 millis)) { res =>


    println("==============================")
    val bodyAsStr = contentAsString(result)
    println("Body as string: " + bodyAsStr)

    val bodyAsJsValue = contentAsJson
    println("body as js: " + bodyAsJsValue)
    println("==============================")

希望这就是您要找的。