我正在为我的网站使用Json.Net。我想让序列化程序默认将属性名称序列化为camelcase。但我不想更改我手动分配的属性名称。我有以下代码:
public class TestClass
{
public string NormalProperty { get; set; }
[JsonProperty(PropertyName = "CustomName")]
public string ConfiguredProperty { get; set; }
}
public void Experiment()
{
var data = new TestClass { NormalProperty = null,
ConfiguredProperty = null };
var result = JsonConvert.SerializeObject(data,
Formatting.None,
new JsonSerializerSettings {ContractResolver
= new CamelCasePropertyNamesContractResolver()}
);
Console.Write(result);
}
< p > < code > Experiment 的输出是: {"normalProperty":null,"customName":null}
然而,我希望输出结果是:
{"normalProperty":null,"CustomName":null}
这个可行吗?
CamelCasePropertyNamesContractResolver
,仅使用JsonProperty
。 - L.BnormalProperty
在JSON中会变成NormalProperty
。 - Oliver