Nag*_*sha 4 json scala httpresponse akka httpentity
我正在使用 akka.http.scaladsl.model.HttpResponse、HttpEntity。
得到 response 后,它是格式的 responseEntity 类型 (Content-type: 'application/json', {MyJSONHERE})。有没有办法从实体中提取我的json。
我尝试了 entity.getDataBytes ,它以 ByteString 格式提供实体的内容。我想正确读取 JSON 并解析它。有人可以指导我吗?
下面的代码对我有用
entity.dataBytes.runWith(Sink.fold(ByteString.empty)(_ ++ _)).map(_.utf8String) map { result =>
JsonMethods.parse(result)
}
Run Code Online (Sandbox Code Playgroud)
dataBytes返回Source[ByteString, Any],Sink.fold将流的所有部分合并为一个ByteString并utf8String转换ByteString为通常的String。
这里有一些关于HttpEntity 的有用文档。