如何对嵌套 json 数组中的第一个元素使用 DataContract?

How to use a DataContract for the first element in a nested json array?

objective 是指示使用 DataContract 的序列化程序只存储第一个元素,让我可以消除使用 IList<MyObject>

样本JSON

{
    "registrant_contacts": [
        {
            "id": null,
            "type": 1,
            "name": "Dns Admin",
            "organization": "Google Inc.",
            "address": "Please contact contact-admin@google.com, 1600 Amphitheatre Parkway",
            "city": "Mountain View",
            "zip": "94043",
            "state": "CA",
            "country": null,
            "country_code": "US",
            "phone": "+1.6502530000",
            "fax": "+1.6506188571",
            "email": "dns-admin@google.com",
            "url": null,
            "created_on": null,
            "updated_on": null
        }
    ]
}

示例数据合同

[DataContract]
public class RegistrantContact
{

    [DataMember(Name="id")]
    public object id { get; set; }

    [DataMember(Name="type")]
    public int type { get; set; }

    [DataMember(Name="name")]
    public string name { get; set; }

    [DataMember(Name="organization")]
    public string organization { get; set; }

    [DataMember(Name="address")]
    public string address { get; set; }

    [DataMember(Name="city")]
    public string city { get; set; }

    [DataMember(Name="zip")]
    public string zip { get; set; }

    [DataMember(Name="state")]
    public string state { get; set; }

    [DataMember(Name="country")]
    public object country { get; set; }

    [DataMember(Name="country_code")]
    public string country_code { get; set; }

    [DataMember(Name="phone")]
    public string phone { get; set; }

    [DataMember(Name="fax")]
    public string fax { get; set; }

    [DataMember(Name="email")]
    public string email { get; set; }

    [DataMember(Name="url")]
    public object url { get; set; }

    [DataMember(Name="created_on")]
    public object created_on { get; set; }

    [DataMember(Name="updated_on")]
    public object updated_on { get; set; }
}

[DataContract]
public class Example
{

    [DataMember(Name="registrant_contacts")]
    public IList<RegistrantContact> registrant_contacts { get; set; }
}

示例序列化代码

public static async Task<Example> GetExample() {
    Example record = new Example();

    using ( WebClient wc = new WebClient() ) {
        wc.Headers.Add( "Accept", "application/json" );

        try {
            DataContractJsonSerializer ser = new DataContractJsonSerializer( typeof( Example ) );
            using ( Stream s = await wc.OpenReadTaskAsync( "https://example.com/sample.json" ) ) {
                record = ser.ReadObject( s ) as Example;
            }
        } catch ( SerializationException se ) {
            Debug.WriteLine( se.Message );
        } catch ( WebException we ) {
            Debug.WriteLine( we.Message );
        } catch ( Exception e ) {
            Debug.WriteLine( e.Message );
        }
    }
    return record;
}

Objective

改变

public IList<RegistrantContact> registrant_contacts { get; set; }

进入

public RegistrantContact registrant_contacts { get; set; }

通过更改 [DataMember(Name="registrant_contacts")] 行,使其使用 registrant_contacts 数组中的第一个元素作为转换期间的唯一结果。

Result,意味着无需调用 response.registrant_contacts[0] 来访问嵌套属性 idtypename 等 - 我可以简单地调用 response.registrant_contacts

您可以通过两种方式 - 首先是将数据临时存储到某个列表中,然后说 .FirstOrDefault();或者第二种是直接尝试说出来,但我相信你会遇到麻烦。因此,只需创建一个临时列表并从中提取您需要的所有内容。数据合同可以是任何你喜欢的。