【发布时间】:2020-11-09 19:56:20
【问题描述】:
如何在 JSON 中定义一个可以在 C# 中解析的字典?
这是我需要解析的 Json:
"InputRequest":
{
"Switches": {"showButton": true}
}
这是我的例子:
public class InputRequest
{
[JsonProperty(PropertyName="Switches")]
public ReadOnlyDictionary<string, bool> Switches { get; }
}
由于某种原因,它无法解析,并显示 Switches 参数的 null 值。
我的另一种方法是创建一个新参数并将字典作为字符串:
public class InputRequest
{
[JsonProperty(PropertyName="Switches")]
public string Switches { get; }
public ReadOnlyDictionary<string, bool> SwitchesDictionary
{
var values = JsonConvert.DeserializeObject<ReadOnlyDictionary<string, bool>>(Switches);
return values;
}
}
对于这种方法,它显示错误
解析 Switch 的值时遇到意外字符
我在这里做错了什么?
【问题讨论】:
标签: c# json getter-setter json-serialization