本文介绍了下载Blob存储并返回Json对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用Newtonsoft.Json
下载存储在Azure Storage
容器中的.json
blob,并将其写入对象.
I am trying to download a .json
blob that I have stored in a container in the Azure Storage
using Newtonsoft.Json
to write it to an object.
我通过致电:
(CloudBlockBlob) blob.DownloadToStream(stream);
但是,我不想将流写入本地应用程序目录中的文件中,而是要返回json object
做Json(result)
However, instead of writing the stream to a file in the local app directory, I want to return the json object
doing Json(result)
这是我尝试过的:
using (var stream = new MemoryStream())
{
blob.DownloadToStream(stream);
var serializer = new JsonSerializer();
using (var sr = new StreamReader(stream))
{
using (var jsonTextReader = new JsonTextReader(sr))
{
result = serializer.Deserialize(jsonTextReader);
}
}
}
最后,我的jsonTextReader
变量为空,而对象null
At the end my jsonTextReader
variable is empty and the object null
我该怎么做?
谢谢
推荐答案
在将Blob读入流后,请将流的位置重置为0
.因此您的代码将是:
Please reset the stream's position to 0
after reading the blob into the stream. So your code would be:
using (var stream = new MemoryStream())
{
blob.DownloadToStream(stream);
stream.Position = 0;//resetting stream's position to 0
var serializer = new JsonSerializer();
using (var sr = new StreamReader(stream))
{
using (var jsonTextReader = new JsonTextReader(sr))
{
var result = serializer.Deserialize(jsonTextReader);
}
}
}
这篇关于下载Blob存储并返回Json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!