38

如何将通用 JObject 转换为 camelCase 纯 json 字符串?我试过 JsonSerializerSettings 但不起作用(Newtonsoft.Json 4.5.11)

[Test]
public void should_convert_to_camel_case()
{
    var serializer = JsonSerializer.Create(new JsonSerializerSettings()
    {
        ContractResolver = new CamelCasePropertyNamesContractResolver()
    });

    var jo = new JObject();
    jo["CamelCase"] = 1;

    var stringWriter = new StringWriter();
    var writer = new JsonTextWriter(stringWriter);
    serializer.Serialize(writer,jo);

    var serialized = stringWriter.ToString();

    Assert.AreEqual("{\"camelCase\":1}", serialized);
}

更新 根据http://json.codeplex.com/workitem/23853无法完成(tnx 到 @nick_w 的链接)

4

4 回答 4

46

这个问题从一个 JObject 开始,想要处理一个驼峰式 JSON 对象。如果您实际上是从一个对象开始并想要获得一个已经被驼峰​​化的 JObject,那么您可以这样做:

var serializer = new JsonSerializer()
{
    ContractResolver = new CamelCasePropertyNamesContractResolver()
};
var jo = JObject.FromObject(someDataContract, serializer);

由此产生的 'jo' 将是驼峰式的。

于 2015-01-26T19:13:42.590 回答
22

根据this Json.NET issue,以JObject这种方式序列化a时,合同解析器被忽略:

在序列化 JObject 时,合同解析器似乎被忽略了。这肯定不是它应该的样子吗? JamesNK
于 2013 年 1 月 30 日上午 8:50关闭这确实有道理,但恐怕这是一个太大的破坏性变化。

受该页面上解决方法的启发,您可以执行以下操作:

var jo = new JObject();
jo["CamelCase"] = 1;

string json = JsonConvert.SerializeObject(jo);
var jObject = JsonConvert.DeserializeObject<ExpandoObject>(json);

var settings = new JsonSerializerSettings()
{
    ContractResolver = new CamelCasePropertyNamesContractResolver()
};

var serialized = JsonConvert.SerializeObject(jObject, settings);

Assert.AreEqual("{\"camelCase\":1}", serialized);

编辑:

关于Dictionary<string, object>. 因此,这样做会跳过额外的 .NET JsonConvert.SerializeObject,但它也减少了对 .NET 的需求ExpandoObject,如果您使用的是 .NET 3.5,这很重要。

Dictionary<string, object> jo = new Dictionary<string, object>();
jo.Add("CamelCase", 1);

var settings = new JsonSerializerSettings()
{
    ContractResolver = new CamelCasePropertyNamesContractResolver()
};

var serialized = JsonConvert.SerializeObject(jo, settings);

Assert.AreEqual("{\"camelCase\":1}", serialized);
于 2013-02-27T07:46:58.277 回答
6

截至2013 年 5 月 8 日,James Newton-King发表的关于 Json.NET 5.0 版本的博客文章已通过添加“DefaultSettings”得到解决。该页面的示例如下,但请阅读该页面以了解第 3 方库的详细信息。

// settings will automatically be used by JsonConvert.SerializeObject/DeserializeObject
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
    {
    Formatting = Formatting.Indented,
    ContractResolver = new CamelCasePropertyNamesContractResolver()
    };

Employee e = new Employee
    {
    FirstName = "Eric",
    LastName = "Example",
    BirthDate = new DateTime(1980, 4, 20, 0, 0, 0, DateTimeKind.Utc),
    Department = "IT",
    JobTitle = "Web Dude"
    };

string json = JsonConvert.SerializeObject(e);
// {
//   "firstName": "Eric",
//   "lastName": "Example",
//   "birthDate": "1980-04-20T00:00:00Z",
//   "department": "IT",
//   "jobTitle": "Web Dude"
// }
于 2015-06-25T18:19:20.657 回答
0
public static JsonSerializer FormattingData()
{
   var jsonSerializersettings = new JsonSerializer {
   ContractResolver = new CamelCasePropertyNamesContractResolver() };
   return jsonSerializersettings;
}


public static JObject CamelCaseData(JObject jObject) 
{   
   var expandoConverter = new ExpandoObjectConverter();
   dynamic camelCaseData = 
   JsonConvert.DeserializeObject(jObject.ToString(), 
   expandoConverter); 
   return JObject.FromObject(camelCaseData, FormattingData());

}

于 2018-11-20T13:03:19.070 回答