JObject&;使用JSON进行CamelCase转换.净值
本文关键字:进行 CamelCase 转换 净值 JSON 使用 amp JObject | 更新日期: 2023-09-27 18:01:04
如何将通用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(
这个问题从一个JObject开始,想要处理一个骆驼大小写的JSON对象。如果你实际上是从一个对象开始,并且想要到达一个已经基于camelbased的JObject,那么你可以这样做:
var serializer = new JsonSerializer()
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
var jo = JObject.FromObject(someDataContract, serializer);
由此产生的"jo"将以骆驼为基础。
根据这个Json。NET问题,当以这种方式序列化JObject
时,合同解析程序被忽略:
序列化JObject时,契约解析器似乎被忽略了。这肯定不是应该的样子吗?
2013年1月30日上午8:50由JamesNK关闭
这确实有道理,但恐怕这是一个太大的突破性变化。
受该页面上的变通方法的启发,您可以这样做:
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>
的优点。因此,这样做可以跳过额外的JsonConvert.SerializeObject
,但也减少了对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年5月8日,James Newton King关于Json 5.0版本的博客文章。NET,这已经通过添加"DefaultSettings"来解决。下面是该页中的示例,但请阅读该页以了解第三方库的详细信息。
// 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"
// }
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());
}