.NET 6-骆驼无效
我曾尝试使用.NET 6上的Camelcase Insentive用于
我在startup.cs中像这样配置的API的内容,但是
.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
options.JsonSerializerOptions.IgnoreNullValues = true;
});
我无法使用此分辨率来解决: https://github.com/andre-ss6 https:///github.com/github.com/dotnet/dotnet/runtime/runtime/31094/suecomment/31094#issuecomment/ 543342051
他建议使用以下代码:
((JsonSerializerOptions)typeof(JsonSerializerOptions)
.GetField("s_defaultOptions",
System.Reflection.BindingFlags.Static |
System.Reflection.BindingFlags.NonPublic).GetValue(null))
.PropertyNameCaseInsensitive = true;
我尝试和工作,但我认为很复杂,因为它使用了反射,我不知道该怎么想,有人有其他解决方案或解释?
我可以这样对其进行审理:
var content = await response.Content.ReadAsStringAsync(cancellationToken);
var result = JsonSerializer.Deserialize<InvestimentFundsResponseData>(content);
我的班级是,你怎么看,我不使用属性[jsonpropertyname]
public class InvestimentFundsResponseData
{
public IEnumerable<InvestmentFundsResponse> Data { get; set;}
}
public class InvestmentFundsResponse
{
public Guid Id { get; set; }
}
I have tried use camelCase insentive on .NET 6 for deseralize content from API
I configured like this in Startup.cs, but it is not working
.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
options.JsonSerializerOptions.IgnoreNullValues = true;
});
I get to solve with this resolution: https://github.com/andre-ss6
https://github.com/dotnet/runtime/issues/31094#issuecomment-543342051
He recommended using the following code:
((JsonSerializerOptions)typeof(JsonSerializerOptions)
.GetField("s_defaultOptions",
System.Reflection.BindingFlags.Static |
System.Reflection.BindingFlags.NonPublic).GetValue(null))
.PropertyNameCaseInsensitive = true;
I tried and worked, but I thought is complex, because it is used reflection, I don't know what to thought, Someone have other solution or a explanation?
I deserialize it like this:
var content = await response.Content.ReadAsStringAsync(cancellationToken);
var result = JsonSerializer.Deserialize<InvestimentFundsResponseData>(content);
My class is, how can you saw, I don't use the attribute [JsonPropertyName]
public class InvestimentFundsResponseData
{
public IEnumerable<InvestmentFundsResponse> Data { get; set;}
}
public class InvestmentFundsResponse
{
public Guid Id { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
jsonserializer.Deserialize
不使用jSonserialializaizeOptions
由addjsonoptions
配置> jsonoptions ):JsonSerializer.Deserialize
does not useJsonSerializerOptions
which are configured byAddJsonOptions
, create and pass required options manually (possibly resolve ones from the DI viaJsonOptions
):我遇到了同样的问题,所以我删除了system.net.json 5.0.2,并重新安装了6.0.2版,这为我解决了。
I had the same issue, so I removed System.Net.Json 5.0.2 and reinstalled the version 6.0.2 this solved it for me.
您也可以制作一个jsonserialialialoptions singleton,然后将其添加到di:
然后使用,只需将其注入构造函数即可。这样,对选项的一个更改就可以通过您的所有代码进行。
注意:我更新了选项,以符合最新版本的system.text.json(6.0.9),并添加了一些我通常指定的。默认的MaxDepth是3,因此,如果您的父/子关系更深,则会错过一些数据。
You could also make a JsonSerializerOptions singleton and add that to DI:
Then to use, simply inject it into the constructor. That way, one change to the options is carried through all your code.
Note: I updated the options to be compliant with the latest version of System.Text.Json (6.0.9) and added a few that I usually specify. The default MaxDepth is 3, so if you have deeper parent/child relationships than that it'll miss some data.