将json反序列化为C#对象时出现问题
我正在调用一个API,但之后反序列化它时遇到了一些问题 以下是我正在尝试做的:将json反序列化为C#对象时出现问题,c#,json,C#,Json,我正在调用一个API,但之后反序列化它时遇到了一些问题 以下是我正在尝试做的: var result = response.Content.ReadAsStringAsync().Result; dynamic data = JObject.Parse(result); var contacts = JsonConvert.DeserializeObject<List<Contact>>(data.c
var result = response.Content.ReadAsStringAsync().Result;
dynamic data = JObject.Parse(result);
var contacts = JsonConvert.DeserializeObject<List<Contact>>(data.contacts);
return contacts;
这是我试图将其反序列化到的Contact类。我使用json2csharp对其进行了转换:
public class Contact
{
public string id { get; set; }
public string type { get; set; }
public string organizationId { get; set; }
public DateTime createdTime { get; set; }
public string name { get; set; }
public string countryId { get; set; }
public string street { get; set; }
public string cityId { get; set; }
public string cityText { get; set; }
public string stateId { get; set; }
public string stateText { get; set; }
public string zipcodeId { get; set; }
public string zipcodeText { get; set; }
public string contactNo { get; set; }
public string phone { get; set; }
public string fax { get; set; }
public string currencyId { get; set; }
public string registrationNo { get; set; }
public string ean { get; set; }
public string localeId { get; set; }
public bool isCustomer { get; set; }
public bool isSupplier { get; set; }
public int paymentTermsDays { get; set; }
public string accessCode { get; set; }
public string emailAttachmentDeliveryMode { get; set; }
public bool isArchived { get; set; }
public bool isSalesTaxExempt { get; set; }
public string defaultExpenseProductDescription { get; set; }
public string defaultExpenseAccountId { get; set; }
public string defaultTaxRateId { get; set; }
}
但它不允许我反序列化它。除了调用API之外,我没有访问该API的权限。是否尝试将paymentTermsDays属性设置为int?因为null值未设置int属性。听起来像是
数据。联系人
不是字符串。当原始字符串被解析为数据
时,它变成了一个动态
,表示对象本身,而不是JSON字符串
我会完全跳过dynamic
部分,只对整个JSON进行反序列化。您不需要所有属性,如果您只需要联系人
,则只需在包含的类中包含该属性即可:
public class Container
{
public IList<Contact> contacts { get; set; }
}
var result = response.Content.ReadAsStringAsync().Result;
var container = JsonConvert.DeserializeObject<Container>(result);
return container.contacts;
其他观察结果:这看起来也不管用:
public int paymentTermsDays { get; set; }
因为数据为空:
"paymentTermsDays": null,
int
不能为null
。反序列化可能会以静默方式将其设置为0
,或者引发错误,但这两种情况都不理想。模型需要与预期数据正确匹配。不要使用int
,而是使用Nullable
:
这可能需要在代码的其他地方进行一些小的更改,以说明此属性是不同的类型。我的解决方案是不创建类:
dynamic data = JObject.Parse(result);
if (data.GetValue("contacts") is JArray contacts) {
return contacts.Select(x => JsonConvert.DeserializeObject<Contact>(x.ToString())).ToList();
}
dynamic data=JObject.Parse(结果);
如果(data.GetValue(“contacts”)为JArray contacts){
返回contacts.Select(x=>JsonConvert.DeserializeObject(x.ToString()).ToList();
}
附言:你应该注意你的模型。你已经知道“付款期限”了。那么“paymentTermsMode”呢?我已经尝试将其更改为字符串,但没有解决任何问题。我不能期望它是无效的json,我假设它不是您实际得到的完整响应?非常感谢,这很有效。我想异步执行,但每次我尝试时,服务器都会返回一些奇怪的ass响应,一旦我更改了它,我就得到了数据,所以我只是假设API不允许访问它async@eeee:这很可能是异步的,并且尝试时出错。你现在所拥有的可能碰巧可以工作,但一旦它停止工作,很容易导致很难诊断错误。谢谢,如果我能修复它,我明天会查看它,否则请联系他们。
public int paymentTermsDays { get; set; }
"paymentTermsDays": null,
public int? paymentTermsDays { get; set; }
dynamic data = JObject.Parse(result);
if (data.GetValue("contacts") is JArray contacts) {
return contacts.Select(x => JsonConvert.DeserializeObject<Contact>(x.ToString())).ToList();
}