【发布时间】:2017-07-05 14:48:48
【问题描述】:
我有一堂课:
public class CustomResponse
{
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string Message {get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string Details {get; set; }
}
然后我尝试将 JSON 字符串反序列化为此类:
var settings = new JsonSerializerSettings
{
NullValueHandling.Ignore,
MissingMemberHandling.Ignore,
};
var customResponse = JsonConvert.Deserialize<CustomResponse>(jsonString, settings);
例如我的 JSON 字符串:
{"DocumentId":"123321", "DocumentNumber":"ABC123"}
结果我有一个对象,它的所有属性都是 NULL,但customResponse 不是 NULL。如何在结果中得到 NULL?
【问题讨论】:
-
你需要一个custom
JsonConverter。 -
可以添加你使用的json字符串吗?
-
为什么你认为
customResponse是null? -
也许
CustomResponse可能有一个计算/只读属性,如IsEmpty或IsValid,用于检测其是否有效。 -
@DanielA.White 如果 CustomResponse 为 NULL,则服务返回正确的响应,我尝试反序列化到 OtherCustomResponse