【发布时间】:2020-02-18 11:11:57
【问题描述】:
我正在尝试序列化一个类中的字典,即使我将 ProcessDictionaryKeys 参数设置为 false,CustomAttributes 字典中的键也会被格式化。
我已经添加了[JsonProperty],如下图:
[JsonProperty(NamingStrategyType = typeof(SnakeCaseNamingStrategy), NamingStrategyParameters = new object[] { false, false })]
public IDictionary<string, string> CustomAttributes { get; set; }
我的 CustomAttributes 数据如下所示:
CustomAttributes = new Dictionary<string, string>()
{
{"Custom Attribute 1", "1"},
{"CustomAttribute 2", "2"}
}
生成的 JSON 如下所示:
custom_attributes\":{\"custom Attribute 1\":\"1\",\"customAttribute 2\":\"2\"
如您所见,每个字典键的第一个字母都没有大写。我怎样才能阻止这种情况发生?
编辑:将 ProcessDictionaryKeys 参数更改为 true 似乎没有任何区别。
【问题讨论】:
-
删除这个
NamingStrategyType = typeof(SnakeCaseNamingStrategy)? -
所有这一切就是将字典的名称更改为默认的驼峰式:
customAttributes\":{\"custom Attribute 1\":\"1\",\"customAttribute 2\":\"2 -
是的,但这是你的问题,不是吗? “如您所见,每个字典键的第一个字母都没有大写。我怎样才能阻止这种情况发生?”
-
不,我的问题与字典中的键有关,而不是字典名称。我需要它们不被格式化,即保持为“自定义属性 1”,而不是格式化为“自定义属性 1”
标签: c# json dictionary serialization json.net