我使用的是akka.http.scaladsl.model.HttpResponse,HttpEntity。
获取响应后,其类型为应用程序类型( responseEntity - type:'application/json',{MyJSONHERE})。有没有办法从实体中提取我的json。
我尝试了entity.getDataBytes,它以ByteString格式提供实体的内容。我想正确地读取JSON并对其进行解析。有人能在这方面给我指点一下吗?
发布于 2017-07-21 14:56:59
下面的代码适用于我
entity.dataBytes.runWith(Sink.fold(ByteString.empty)(_ ++ _)).map(_.utf8String) map { result =>
JsonMethods.parse(result)
}dataBytes返回Source[ByteString, Any],Sink.fold将流的所有部分合并到一个ByteString中,utf8String将ByteString转换为普通的String。
这里有一些关于HttpEntity的有用文档。
发布于 2017-07-19 22:36:05
你能试试下面的代码吗?
entity.getDataBytes.utf8String这将返回JSON的字符串表示。
https://stackoverflow.com/questions/45192752
复制相似问题