5

Here's the JSON response that I got from web service:

{
    "odata.metadata": "https://graph.windows.net/00c6cd14-eaee-4d02-9807-59bcd4f98847/$metadata#domainDnsRecords",
    "value": [{
        "odata.type": "Microsoft.DirectoryServices.DomainDnsTxtRecord",
        "dnsRecordId": "aceff52c-06a5-447f-ac5f-256ad243cc5c",
        "isOptional": false,
        "label": "aro3652698.applabdnstest1.com",
        "recordType": "Txt",
        "supportedService": "Email",
        "ttl": 3600,
        "text": "MS=ms14815229"
    },
    {
        "odata.type": "Microsoft.DirectoryServices.DomainDnsMxRecord",
        "dnsRecordId": "5fbde38c-0865-497f-82b1-126f596bcee9",
        "isOptional": false,
        "label": "aro3652698.applabdnstest1.com",
        "recordType": "Mx",
        "supportedService": "Email",
        "ttl": 3600,
        "mailExchange": "ms14815229.msv1.invalid",
        "preference": 32767
    }]
}

Elements in the array under "value" token are of different derived type.
There's DomainDnsRecord abstract base class, and classes that inherit from base are DomainDnsTxtRecord and DomainDnsMxRecord.
In the given JSON array (but not always!), first element is of DomainDnsTxtRecord and the second one is of DomainDnsMxRecord type.

The field that contains information about what type the given element is, is: "recordType".

What is the easiest, most convenient way to deserialize this JSON array into a list of base classes, e.g. List<DomainDnsRecord> (using Newtonsoft for example)?

Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63
dragan.stepanovic
  • 2,955
  • 8
  • 37
  • 66
  • 1
    Possible duplicates: [Deserializing polymorphic json classes without type information using json.net](http://stackoverflow.com/questions/19307752) or [Json.Net Serialization of Type with Polymorphic Child Object](http://stackoverflow.com/questions/29528648) or [JsonConverter with Interface](https://stackoverflow.com/questions/33321698). – dbc Jan 22 '16 at 16:17
  • Possible duplicate of [Json.Net Serialization of Type with Polymorphic Child Object](http://stackoverflow.com/questions/29528648/json-net-serialization-of-type-with-polymorphic-child-object) – krillgar Jan 09 '17 at 15:27
  • Note to readers: please use the Microsoft Graph API instead of the deprecated Azure AD Graph API. – Michael Mainer Nov 21 '19 at 18:15

0 Answers0