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!
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;
}
}
}
I think the issue is because you're mixing different libraries (Newtonsoft.Json and System.Text.Json).
I did a text using both and they are working fine.
System.Text.Json:
using System;
using System.Text.Json;
using System.Collections;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
string json = "{\"systemId\":\"P0700055\",\"sessionId\":\"ec322078-8934-441e-a090-9c43e255ccf7\",\"startDateTime\":\"2021-04-10T00:13:13.0008388+00:00\",\"timeSpanSec\":30,\"span\":\"00:00:30\",\"signals\":[]}";
var values = JsonSerializer.Deserialize<Dictionary<string, dynamic>>(json);
foreach(var kvp in values)
Console.WriteLine(kvp.Value.ToString());
}
}
Newtonsoft.Json
using System;
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
string json = "{\"systemId\":\"P0700055\",\"sessionId\":\"ec322078-8934-441e-a090-9c43e255ccf7\",\"startDateTime\":\"2021-04-10T00:13:13.0008388+00:00\",\"timeSpanSec\":30,\"span\":\"00:00:30\",\"signals\":[]}";
var values = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(json);
foreach(var kvp in values)
Console.WriteLine(kvp.Value.ToString());
}
}
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.