简体   繁体   English

Azure Blob 列表键 JsonSerializer.Deserialize 返回 null C#

[英]Azure Blob list keys JsonSerializer.Deserialize returning null C#

I have a very simple deserialization to be made.我有一个非常简单的反序列化。 Basically all I do is to retrieve the following json and deserialize as StorageAccountListKeysResult of Microsoft.Azure.Management.Storage.Models :基本上我所做的就是检索以下 json 并反序列化为Microsoft.Azure.Management.Storage.ModelsStorageAccountListKeysResult

{\"creationTime\":\"2021-12-17T12:26:19.5708372Z\",\"keyName\":\"key1\",\"value\":\"qzyyzfNe1bkhBeOVYNXiQ6BjVlDnLsnDWhVqhTrD6whPE78kf/1A/jNeQsUjMvQVnl3dtdKxZmsuMjlx3ru7Q==\",\"permissions\":\"FULL\"},{\"creationTime\":\"2021-12-17T12:26:19.5708372Z\",\"keyName\":\"key2\",\"value\":\"t+4ao7yMyWvqoiSjrNCH5gPtDC7+Qnow2SdPoFXzOXkPyBrOex/BVUPXtGFW78XAXrQ7ITDZnC8RIyXe5jVaQ==\",\"permissions\":\"FULL\"}

I doubled checked the json result several times and compared with the model and it seems all fine to me but I get null as result from deserialization.我多次检查了 json 结果,并与 model 进行了比较,这对我来说似乎很好,但我得到null作为反序列化的结果。

Here is the code:这是代码:

var serializerOptions = new JsonSerializerOptions
{
    PropertyNameCaseInsensitive = true,
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};

var json = "{\"Keys\":[{\"creationTime\":\"2021-12-17T12:26:19.5708372Z\",\"keyName\":\"key1\",\"value\":\"qzyyzfNe1bkhBeOVYNXiQ6BjVlDnLsnDWhVqhTrD6whPE78kf/1A/jNeQsUjMvQVnl3dtdKxZmsuMjlx3ru7Q==\",\"permissions\":\"FULL\"},{\"creationTime\":\"2021-12-17T12:26:19.5708372Z\",\"keyName\":\"key2\",\"value\":\"t+4a7oyMyWvqoiSjrNCH5gPtDC7+Qnow2SdPoFXzOXkPyBrOex/BVUPXtGFW78XAXrQ7ITDZnC8RIyXe5jVaQ==\",\"permissions\":\"FULL\"}]}";
var listKeys = JsonSerializer.Deserialize<StorageAccountListKeysResult>(json, serializerOptions); // returns null

What am I missing?我错过了什么?

EDIT:编辑:

I just realized that the properties of StorageAccountListKeysResult have no setters.我刚刚意识到StorageAccountListKeysResult的属性没有设置器。

Please try by changing your Keys attribute in the JSON to keys .请尝试将 JSON 中的Keys属性更改为keys Your json should look like:您的json应如下所示:

var json = "{\"keys\":[{\"creationTime\":\"2021-12-17T12:26:19.5708372Z\",\"keyName\":\"key1\",\"value\":\"qzyyzfNe1bkhBeOVYNXiQ6BjVlDnLsnDWhVqhTrD6whPE78kf/1A/jNeQsUjMvQVnl3dtdKxZmsuMjlx3ru7Q==\",\"permissions\":\"FULL\"},{\"creationTime\":\"2021-12-17T12:26:19.5708372Z\",\"keyName\":\"key2\",\"value\":\"t+4a7oyMyWvqoiSjrNCH5gPtDC7+Qnow2SdPoFXzOXkPyBrOex/BVUPXtGFW78XAXrQ7ITDZnC8RIyXe5jVaQ==\",\"permissions\":\"FULL\"}]}";

Reference: https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.management.storage.models.storageaccountlistkeysresult.keys?view=azure-dotnet#Microsoft_Azure_Management_Storage_Models_StorageAccountListKeysResult_Keys参考: https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.management.storage.models.storageaccountlistkeysresult.keys?view=azure-dotnet#Microsoft_Azure_Management_KeysResult_Keys_Model

[Newtonsoft.Json.JsonProperty(PropertyName="keys")]
public System.Collections.Generic.IList<Microsoft.Azure.Management.Storage.Models.StorageAccountKey> Keys { get; }

The problem was solved by using Newtonsoft .使用Newtonsoft解决了这个问题。 Microsoft uses Newtonsoft for StorageAccountListKeysResult , so I guess this was the issue.微软使用 Newtonsoft 作为StorageAccountListKeysResult ,所以我想这就是问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM