title: C# and REST - Deserialize JSON object with unknown nesting (type)
body: <p>I consume REST API, which return JSON string. </p>
  
  <p>Example of document:</p>
  
  <pre><code>{
    "kind": "collection#ExtendedACE",
    "selfLink": "https://1.2.3.4/api/access/in/Alohomora/rules",
    "rangeInfo": {
      "offset": 0,
      "limit": 3,
      "total": 3
    },
    "items": [
      {
        "kind": "object#ExtendedACE",
        "selfLink": "https://1.2.3.4/api/access/in/Alohomora/rules/2638577060",
        "permit": false,
        "sourceAddress": {
          "kind": "AnyIPAddress",
          "value": "any"
        },
        "destinationAddress": {
          "kind": "objectRef#NetworkObj",
          "refLink": "https://1.2.3.4/api/objects/networkobjects/facebook.com",
          "objectId": "facebook.com"
        },
        "sourceService": {
          "kind": "NetworkProtocol",
          "value": "tcp"
        },
        "destinationService": [
          {
            "kind": "TcpUdpService",
            "value": "tcp/http"
          },
          {
            "kind": "TcpUdpService",
            "value": "tcp/https"
          }
        ],
        "active": true,
        "remarks": [],
        "ruleLogging": {
          "logInterval": 300,
          "logStatus": "Default"
        },
        "position": 1,
        "isAccessRule": true,
        "objectId": "2638577060"
      },
      {
        "kind": "object#ExtendedACE",
        "selfLink": "https://1.2.3.4/api/access/in/Alohomora/rules/2115120101",
        "permit": false,
        "sourceAddress": {
          "kind": "objectRef#NetworkObjGroup",
          "refLink": "https://1.2.3.4/api/objects/networkobjectgroups/Hermiona-source",
          "objectId": "Hermiona-source"
        },
        "destinationAddress": {
          "kind": "objectRef#NetworkObjGroup",
          "refLink": "https://1.2.3.4/api/objects/networkobjectgroups/Hermiona-destination",
          "objectId": "Hermiona-destination"
        },
        "sourceService": {
          "kind": "NetworkProtocol",
          "value": "icmp"
        },
        "destinationService": {
          "kind": "NetworkProtocol",
          "value": "icmp"
        },
        "active": true,
        "remarks": [
          "Test Hermiona Alohomora kouzlo"
        ],
        "ruleLogging": {
          "logInterval": 300,
          "logStatus": "Default"
        },
        "position": 2,
        "isAccessRule": true,
        "objectId": "2115120101"
      },
    ]
  }
  </code></pre>
  
  <p>The problem are <strong>destinationService</strong>, because I dont know, how can I write the object for Deserialize.</p>
  
  <p><strong>1/ If I use</strong></p>
  
  <pre><code>var varAccessInRulesCollection = (new JavaScriptSerializer()).Deserialize&lt;RootObject&gt;(strAccessInRulesGet).items;
  </code></pre>
  
  <p>where public class contains List</p>
  
  <pre><code>public class Item
      {
          ...
          public List&lt;DestinationService&gt; destinationService { get; set; }
          ...
      }
  </code></pre>
  
  <p>the result is only the first items with collection, the second item is ignored (not parsed)</p>
  
  <p><strong>2/ If I use</strong></p>
  
  <pre><code>var varAccessInRulesCollection = (new JavaScriptSerializer()).Deserialize&lt;RootObject&gt;(strAccessInRulesGet).items;
  </code></pre>
  
  <p>where public class contains object</p>
  
  <pre><code>public class Item
      {
          ...
          public object destinationService { get; set; }
          ...
      }
  </code></pre>
  
  <p>the result is only the second items as array, the first item is ignored (not parsed)</p>
  
  <p>Exists some code, how  can I tell, what I want apply both objects with same name by parsing json definiton? Or how is this problem solved?</p>
  
  <p>Thank you, Petr</p>