Home > Back-end >  How to parse content get from Azure Blob into Dictionary<string, dynamic> using C#?
How to parse content get from Azure Blob into Dictionary<string, dynamic> using C#?

Time:07-14

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!

CodePudding user response:

    private async Task<string> GetBlob(BlobClient blobClient)
    {
        string file = string.Empty;
        MemoryStream fileStream = new MemoryStream();

        try
        {
            using (var stream = new MemoryStream())
            {
                await blobClient.DownloadToAsync(stream).ConfigureAwait(false);

                if (stream != null)
                {

                    stream.WriteTo(fileStream);
                    fileStream.Position = 0;

                    StreamReader reader = new StreamReader(fileStream);
                    file = reader.ReadToEnd();
                }

            }

            return file;
        }
        catch (Exception)
        {
            return string.Empty;
        }

    }
}

CodePudding user response:

To fix the error, simply remove the following 2 lines of code:

var options = new JsonSerializerOptions { WriteIndented = true };
var jsonString = System.Text.Json.JsonSerializer.Serialize(blobContents, options);

and replace the following line of code:

Dictionary<string, dynamic>? values = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(jsonString);

with

Dictionary<string, dynamic>? values = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(blobContents);

So your code would be something like:

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);
            
            Dictionary<string, dynamic>? values = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(blobContents);
}

  • Related