如何在不执行模型绑定的情况下更改json 的属性名的大小写?JsonElement序列化忽略了PropertyNaming JsonSerializer选项,这里也证实了这一点:建议使用JsonNode/JsonObject会导致相同的行为。
有什么暗示我能做到的吗?
举个例子,我想改变这一点:
{
"MyPoperty" : 5,
"MyComplexProperty" : {
"MyOtherProperty": "value",
"MyThirdProperty": true
}
}对此:
{
"myPoperty" : 5,
"myComplexProperty" : {
"myOtherProperty": "value",
"myThirdProperty": true
}
}干杯。
发布于 2021-12-06 09:24:31
我想你试着用Newtonsoft json
class Person
{
public string UserName { get; set; }
public int Age { get; set; }
}编码
static void Main(string[] args)
{
Person person = new Person();
person.UserName = "Bob";
person.Age = 20;
var serializerSettings = new JsonSerializerSettings();
serializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
var json = JsonConvert.SerializeObject(person, serializerSettings);
Console.WriteLine(json);
}输出
{"userName":"Bob","age":20}发布于 2021-12-07 03:11:17
不依赖于Newtonsoft json,而是依赖于多层对象。
var json = @"{""ShouldWindUpAsCamelCase"":""does it?""}";
var obj = JsonSerializer.Deserialize<Dictionary<string,string>>(json);
var dic = new Dictionary<string, string>();
foreach (var item in obj)
{
dic.Add(item.Key.FirstCharToLower(), item.Value);
}
var serialized = System.Text.Json.JsonSerializer.Serialize(dic);
Console.WriteLine(serialized);FirstCharToLower()函数
public static string FirstCharToLower(this string input)
{
if (String.IsNullOrEmpty(input))
return input;
string str = input.First().ToString().ToLower() + input.Substring(1);
return str;
}#输出
{"shouldWindUpAsCamelCase":"does it?"}https://stackoverflow.com/questions/70242842
复制相似问题