I am trying to get contents from blob in Azure which is key-value pairs and transfer them into Dictionary<String, dynamic>. But it falled.
My code is as follows:
static async Task Main()
{
BlobServiceClient blobServiceClient = new BlobServiceClient("#");
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("#");
var blobClient = containerClient.GetBlobClient("#");
var response = blobClient.DownloadContent();
var data = response.Value.Content;
var blobContents = Encoding.UTF8.GetString(data);
var options = new JsonSerializerOptions { WriteIndented = true };
var jsonString = System.Text.Json.JsonSerializer.Serialize(blobContents, options);
Dictionary<string, dynamic>? values = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(jsonString);
}
But it shows that :
unhandled exception. Newtonsoft.Json.JsonSerializationException: Error converting value "{"key":"value","...} to type 'System.Collections.Generic.Dictionary`2[System.String,System.Object]'. Path '', line 1, position 104484.
I want to know how to fix it.Thank you!
2
Answers
To fix the error, simply remove the following 2 lines of code:
and replace the following line of code:
with
So your code would be something like: