这是this question(它的一个子集)绝对最简单的版本.
using System;
using System.Collections.Generic;
using System.Text.Json;
public class Program
{
public class Subscription
{
public bool HasRead { get; set; } = true;
public string TimeStamp { get; set; } = "";
}
public static void Main()
{
// this input format is a requirement. It cannot be changed.
string json = @"
{
""305FDF11-25E7-43DE-B09D-DFFC17C79599"": {
""hasRead"": true, // EDIT: Removed the double quotes around the boolean. That was not the core issue.
""Timestamp"": ""XXX""
}
}
";
// I'm trying to find the correct deserialization type in C#
var deser = JsonSerializer.Deserialize<KeyValuePair<string, Subscription>>(json,
new JsonSerializerOptions(JsonSerializerDefaults.Web));
// System.Text.Json.JsonException:
//'The JSON value could not be converted to
//System.Collections.Generic.KeyValuePair
}
}
我不明白为什么它不能被反序列化.
注:我痴迷于KeyValuePair
型,但可能与油田的shell 或其他明显的东西有关.
其他注意事项:当我将KeyValuePair<string, Subscription>
更改为KeyValuePair<string, object>
以使其更宽松时,它仍然失败.