如何在自定义 C# class 中为具有 Outlook REST API 的联系人序列化字符串集合?

How are string collections serialized in a custom C# class for Contacts with the Outlook REST API?

我使用带联系人的 Graph Explorer 从示例 JSON 输出生成了 C# class(如下所示)。但是,当我将字符串数组属性的空值的序列化对象(使用 JsonConvert.SerializeObject(myclass))传递给 POST 操作以创建联系人时,我在回应:

A null value was found for the property named 'BusinessPhones', which has the expected type 'Collection(Edm.String)[Nullable=False]'. The expected type 'Collection(Edm.String)[Nullable=False]' does not allow null values.

(供参考,这些是受支持的联系人属性:https://msdn.microsoft.com/en-us/office/office365/api/complex-types-for-mail-contacts-calendar#contact

这是原始 JSON:

的示例
{"AssistantName":null,"Birthday":null,"BusinessAddress":null,"BusinessPhones":null,"Categories":null,"Children":null,"CompanyName":"Fun Toys and Bikes","Department":null,"DisplayName":"Mr. Robert E. Ahlering","EmailAddresses":[{"Address":"robert@funtoys.com","Name":"Robert E. Ahlering (robert@funtoys.com)"}],"FileAs":null,"Generation":null,"GivenName":"Robert","HomeAddress":null,"Initials":null,"JobTitle":"Owner","Manager":null,"MiddleName":null,"MobilePhone1":null,"NickName":null,"OfficeLocation":null,"OtherAddress":null,"PersonalNotes":null,"Profession":null,"SpouseName":null,"Surname":"Ahlering","Title":null,"YomiCompanyName":null,"YomiGivenName":null,"YomiSurname":null}

我认为空 BusinessPhones 值(选择字符串数组之一)的正确格式应该是 "BusinessPhones":[],而不是 "BusinessPhones:null".

那么我如何在 class 中实现字符串 array/collection 属性来适应这个?

public partial class OutlookContact
{
    public partial class Businessaddress
    {
        public object City { get; set; }

        public object CountryOrRegion { get; set; }

        public object PostalCode { get; set; }

        public object State { get; set; }

        public object Street { get; set; }
    }

    public partial class Emailaddress
    {
        public string Address { get; set; }

        public string Name { get; set; }
    }

    public partial class Homeaddress
    {
        public string City { get; set; }

        public object CountryOrRegion { get; set; }

        public string PostalCode { get; set; }

        public string State { get; set; }

        public string Street { get; set; }
    }

    public partial class Otheraddress
    {
        public object City { get; set; }

        public object CountryOrRegion { get; set; }

        public object PostalCode { get; set; }

        public object State { get; set; }

        public object Street { get; set; }
    }

    public partial class Rootobject
    {
        public object AssistantName { get; set; }

        public DateTime? Birthday { get; set; }

        public Businessaddress BusinessAddress { get; set; }

        public string[] BusinessPhones { get; set; }

        public object[] Categories { get; set; }

        public object[] Children { get; set; }

        public object CompanyName { get; set; }

        public string Department { get; set; }

        public string DisplayName { get; set; }

        public Emailaddress[] EmailAddresses { get; set; }

        public string FileAs { get; set; }

        public object Generation { get; set; }

        public string GivenName { get; set; }

        public Homeaddress HomeAddress { get; set; }

        public object Initials { get; set; }

        public string JobTitle { get; set; }

        public object Manager { get; set; }

        public object MiddleName { get; set; }

        public object MobilePhone1 { get; set; }

        public object NickName { get; set; }

        public string OfficeLocation { get; set; }

        public Otheraddress OtherAddress { get; set; }

        public object PersonalNotes { get; set; }

        public object Profession { get; set; }

        public object SpouseName { get; set; }

        public string Surname { get; set; }

        public object Title { get; set; }

        public object YomiCompanyName { get; set; }

        public object YomiGivenName { get; set; }

        public object YomiSurname { get; set; }
    }
}

来自错误null 不允许收集。您应该可以改用任何空集合:

"BusinessPhones": []

您需要在 RootObject class 的构造函数中将 BusinessPhones 属性 初始化为一个空数组,如下所示。

public partial class RootObject
{
    public RootObject()
    {
        this.BusinessPhones = new string[0];
    }

    // rest of the class...
}

如果这不可能,您需要确保在将 BusinessPgones 序列化为 JSON 字符串之前将其初始化为空数组。

var rootObject = new RootObject();
rootObject.BusinessPhones = new string[];
// set other properties of rootObject
//Serialize rootObject to JSON
// Call rest API.

这应该可以解决您的问题。