我正在使用Json.NET进行我正在进行的项目.从外部API,我接收的JSON具有作为对象的属性,但是当它们为空时,传递"false".
例如:
data: {
supplier: {
id: 15,
name: 'TheOne'
}
}
Run Code Online (Sandbox Code Playgroud)
也可能是:
data: {
supplier: false
}
Run Code Online (Sandbox Code Playgroud)
我应该如何定义供应商属性,以便将供应商反序列化为Supplier对象或为null.
现在我有:
public class Data {
[JsonProperty("supplier")]
public SupplierData Supplier { get; set; }
}
public class SupplierData {
[JsonProperty("id")]
public int Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
但是现在当供应商的值为'false'时尝试反序列化时,它会失败.当JSON值为"false"时,我希望Supplier属性为null.
我希望有人知道如何做到这一点.谢谢.
我有以下内容json被发送到 api
{
"Id":22,
"UserId":22,
"Payload":{
"Field":"some payload value"
...more unknown field/values go here
},
"ContextTypeId":1,
"EventTypeId":1,
"SessionId":1
}
Run Code Online (Sandbox Code Playgroud)
我想将其映射到以下内容:
public class CreateTrackItem : IRequest<int>
{
public int Id { get; set; }
public int UserId { get; set; }
public string Payload { get; set; }
public int ContextTypeId { get; set; }
public int SessionId { get; set; }
public int EventTypeId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
当映射Payload属性失败,它不能映射json到字符串,我只是想Payload成为一个string版本 …