使用 Json.NET 的 WebApi2 控制器无法反序列化单个 属性
WebApi2 Controller using Json.NET failing to deserialize single property
当向使用 Json.NET 进行反序列化的 WebApi2 控制器发送带有已确认格式正确的对象的 POST 请求时,未正确解析单个 属性。
请求中发出的JSON如下:
{"ParentId":8,"Data":{"Description":"Estimated Man Hours ONSITE","ExternalItemKey":"Services:Man Hours","Quantity":0,"Price":150,"EstimatedCost":60},"ActualCost":60,"DateProvided":"2015-09-18T00:26:45.000Z","ReferencedQuotedDeliverableExternalGuid":"1b7e42be-670e-4d84-9600-15dff530363e","Notes":""}
Data.EstimatedCost 值反序列化为 0,可能是因为某些原因未对其进行解析。所有其他值都被正确解析。
Json.NET的版本是6.0.0.0
我为此找到的解决方案是使用以下语法标记未能按要求反序列化的变量:
using Newtonsoft.Json;
public class Deliverable
{
[JsonProperty(Required = Required.Always)]
public decimal EstimatedCost { get; private set; }
}
当向使用 Json.NET 进行反序列化的 WebApi2 控制器发送带有已确认格式正确的对象的 POST 请求时,未正确解析单个 属性。
请求中发出的JSON如下:
{"ParentId":8,"Data":{"Description":"Estimated Man Hours ONSITE","ExternalItemKey":"Services:Man Hours","Quantity":0,"Price":150,"EstimatedCost":60},"ActualCost":60,"DateProvided":"2015-09-18T00:26:45.000Z","ReferencedQuotedDeliverableExternalGuid":"1b7e42be-670e-4d84-9600-15dff530363e","Notes":""}
Data.EstimatedCost 值反序列化为 0,可能是因为某些原因未对其进行解析。所有其他值都被正确解析。
Json.NET的版本是6.0.0.0
我为此找到的解决方案是使用以下语法标记未能按要求反序列化的变量:
using Newtonsoft.Json;
public class Deliverable
{
[JsonProperty(Required = Required.Always)]
public decimal EstimatedCost { get; private set; }
}