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

241 views Asked by At

The objective is to instruct the serializer using DataContract to store the first element only, allowing me to eliminate the use of IList<MyObject>

Sample JSON

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

Sample DataContract

[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; }
}

Sample Serialize Code

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

To change

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

Into

public RegistrantContact registrant_contacts { get; set; }

By altering the [DataMember(Name="registrant_contacts")] line so it uses the first element from the registrant_contacts array as the only result during the transform.

Result, means that instead of calling response.registrant_contacts[0] to access the nested properties id, type, name, etc - I can simply call response.registrant_contacts

1

There are 1 answers

3
Stoyan Bonchev On BEST ANSWER

You can in two ways - first is to temporary store the data into some list and then to say .FirstOrDefault(); or second is directly to try saying it, but I believe you will have complications. So, just create a temporary list and the extract everything what you need from it. The the data contract could be whatever you like.