我正在使用akka.http.scaladsl.model.HttpResponse,HttpEntity。從HttpEntity獲取JSON
得到響應後,它的格式爲responseEntity(Content-type:'application/json',{MyJSONHERE})。有沒有一種方法可以從實體中提取我的json?
我試過entity.getDataBytes,它以ByteString格式給出了實體的內容。我想正確閱讀JSON並解析它。有人可以指導我嗎?
我正在使用akka.http.scaladsl.model.HttpResponse,HttpEntity。從HttpEntity獲取JSON
得到響應後,它的格式爲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)
}
dataBytes
回報Source[ByteString, Any]
,Sink.fold
結合流的所有部分納入一個ByteString
和utf8String
轉換成ByteString
通常String
。
以下是關於HttpEntity的一些有用的文檔。
你可以試試下面的代碼嗎?
entity.getDataBytes.utf8String
這將返回JSON的字符串表示形式。下面
utfString是沒有得到公認的有效功能 –
用於訪問'utf8String','entity.getDataBytes'必須返回字節字符串和字節字符串類有UTF8字符串函數。 –
你正在使用'utfString'而不是'utf8String' –
相關 - https://stackoverflow.com/q/32315789/864369 –