Reputation: 1759
I am using azure blob storage to store a JSON file and in my code I download it using the C# APIs.
When I download the contents of the text file and attempt to Deserialize it using JsonConvert, I get an error (the Visual Studio JSON visualizer also shows an error for the text). However, if I copy the text and paste into JSONLint.com it appears fine. Also, If I manually download the file from Azure Storage and read the file in code, it deserializes just fine. Any ideas why I am getting invalid data when I download using the C# APIs?
var storageAccount = CloudStorageAccount.Parse(connectionString);
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference(containerName);
var blob = container.GetBlockBlobReference(folderAndFileName);
var text = await blob.DownloadTextAsync();
var obj = JsonConvert.DeserializeObject(text);
// Exception: "Unexpected character encountered while parsing value: . Path '', line 0, position 0."
Note: I am uploading the file via Powershell:
$blobProperties = @{"ContentType" = "application/json"};
Set-AzureStorageBlobContent -Container $containerName -File $LocalFilePath -Blob $RemoteBlobName -BlobType "Block" -Properties $blobProperties
Upvotes: 6
Views: 1572
Reputation: 1759
After further investigation, I found that the downloaded file has an extra Unicode formatting character at the start. I added the following code to just check for special characters at the beginning and remove them...
var startIndex = 0;
while (char.GetUnicodeCategory(text, startIndex) == UnicodeCategory.Format)
{
startIndex++;
}
text = text.Substring(startIndex, text.Length - startIndex);
Upvotes: 3
Reputation: 3293
I have tested your code. It seems that this issue is not about your code. The following is my tested result with your code:
Entity:
public class People
{
[JsonProperty(PropertyName ="name")]
public string Name { get; set; }
[JsonProperty(PropertyName = "address")]
public string Address { get; set; }
}
Json File:
{
"name": "jambor",
"address": "us"
}
As Gaurav Mantri said, I would suggest you check your storage library. The version of my Azure storage library is 7.2.1. Please also set breakpoint at the code var obj = JsonConvert.DeserializeObject(text);
. Then check the value of text. It may help you to find out the issue.
Upvotes: 1