分析scala中的HTTP POST响应JSON正文



我正在发送一个http post请求,如下所示:

def Token(url: String, Id: String, key: String): String = {
val body =
s"""
| "id": ${Id}
| "key": ${key}
|""".stripMargin
val request = Http(url).postData(body)
.header("content-type", "application/json")
.option(HttpOptions.method("POST"))
val response = request.execute()
}

我的回复主体形式为:

{
"token": "xyz",
"abc": "defgh"
}

我想解析这个响应以获得Scala中"token"("xyz"(的值。你是怎么做到的?

有一种语法可以在Play框架上使用,如下所示:

response =>
val json = response.json
println (json  "error").asOpt[String]

你可以在这里阅读更多信息:

https://www.playframework.com/documentation/2.8.x/ScalaJsonHttp

最新更新