首页 > 解决方案 > 返回时我收到错误`.. ASP.NET

问题描述

var result = JsonConvert.DeserializeObject<Risks>(jsonData);
             var finalResult = result.Value.Where(x => x.ID == 5).ToList();
             return finalResult;
    

我正在开发一个返回<IEnumerable<RiskValue>反序列化不会导致任何错误的 API,当我返回最终结果时会显示此错误,知道是什么原因造成的吗?

System.InvalidOperationException: The JSON property name for 'HRDF_1.Models.RisksValue.ID' collides 
with another property.

System.InvalidOperationException: The JSON property name for 'test_1.Models.RisksValue.ID' collides with another property.
   at System.Text.Json.ThrowHelper.ThrowInvalidOperationException_SerializerPropertyNameConflict(Type type, JsonPropertyInfo jsonPropertyInfo)

标签: c#jsonasp.net-core

解决方案


尝试在您的 Startup.cs中设置PropertyNameCaseInsensitive为:false

services.AddControllers().AddJsonOptions(options => { 
    options.JsonSerializerOptions.PropertyNamingPolicy = null; 
    
    //the importance here..
    options.JsonSerializerOptions.PropertyNameCaseInsensitive = false;
});

推荐阅读