1

像这样使用 JSON.Net:

JsonConvert.SerializeObject(someObject, 
                            Newtonsoft.Json.Formatting.None, 
                            new JsonSerializerSettings() {
                                NullValueHandling = NullValueHandling.Ignore,
                                ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                                ContractResolver = new CamelCasePropertyNamesContractResolver()
                            });

JSON.Net 做了多少骆驼案?

它只是从单词的开头开始的小写字母吗?

例子:

  • somePropertyId -> somePropertyId
  • somePropertyID -> somePropertyID
  • SOMEPropertyID -> somePropertyID
  • SOMEPROPERTYID -> somepropertyid
4

1 回答 1

6

这是 CamelCasePropertyNamesContractResolver 所做的,直接来自单元测试:https ://github.com/JamesNK/Newtonsoft.Json/blob/95665429a431364327b4bce5332c39fda7819e7b/Src/Newtonsoft.Json.Tests/Utilities/StringUtilsTests.cs#L40-L54

[Test]
public void ToCamelCaseTest()
{
  Assert.AreEqual("urlValue", StringUtils.ToCamelCase("URLValue"));
  Assert.AreEqual("url", StringUtils.ToCamelCase("URL"));
  Assert.AreEqual("id", StringUtils.ToCamelCase("ID"));
  Assert.AreEqual("i", StringUtils.ToCamelCase("I"));
  Assert.AreEqual("", StringUtils.ToCamelCase(""));
  Assert.AreEqual(null, StringUtils.ToCamelCase(null));
  Assert.AreEqual("iPhone", StringUtils.ToCamelCase("iPhone"));
  Assert.AreEqual("person", StringUtils.ToCamelCase("Person"));
  Assert.AreEqual("iPhone", StringUtils.ToCamelCase("IPhone"));
  Assert.AreEqual("i Phone", StringUtils.ToCamelCase("I Phone"));
  Assert.AreEqual(" IPhone", StringUtils.ToCamelCase(" IPhone"));
}
于 2013-05-09T23:23:19.490 回答