首页 > 解决方案 > 使用 CRM 365 API 反序列化 json c#

问题描述

我有一个这种格式的 JSON 字符串:

所以,我做了这样的课程:

public class Values
{
    public string odata_context { get; set; }
    public List<ContactsDeserialize> keyValues { get; set; }
}
public class ContactsDeserialize : IDisposable
{
    public string odata_etag     { get; set; }
    public Guid contactid        { get; set; }
    public string crimson_title  { get; set; }
    public string firstname      { get; set; }
    public string lastname       { get; set; }
    public DateTime? createdon   { get; set; }
    public DateTime? birthdate   { get; set; }
    public string emailaddress1  { get; set; }
    public string mobilephone    { get; set; }
    public string address1_line1 { get; set; }
    public string address1_line2 { get; set; }
    public string address1_line3 { get; set; }
    public string address1_city  { get; set; }
    public string address1_postalcode { get; set; }
    public string address1_telephone1 { get; set; }
}

但是当我尝试通过使用反序列化它时

Values ContactData = JsonConvert.DeserializeObject<Values>(jsonstring);

它返回空值。查不出原因

谢谢

标签: c#asp.net.netdynamiccrm

解决方案


您的类字段与原始 json 字段略有不同,因此如果您想使用它,您需要将属性放入您的字段以提供正确的 json 字段,即

[JsonProperty("@odata.etag")]
public string odata_etag { get; set; }

有关更多详细信息,请参阅文档文章


推荐阅读