title: Newtonsoft JSON deserialize using HttpWebResponse
body: <p>I was searching in all the questions about deserialization with Newtonsfot Json Converter. But i wasn't able to find the problem in my code. So I put here, asking for help.</p>
  
  <p>The message error from the visual studio:</p>
  
  <blockquote>
    <p>No se controló Newtonsoft.Json.JsonSerializationException
          HResult=-2146233088
          Message=Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'APIEffilogics.Usuari+Client' because the type requires a
    JSON object (e.g. {"name":"value"}) to deserialize correctly.
        To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a
    type that implements a collection interface (e.g. ICollection, IList)
    like List that can be deserialized from a JSON array.
    JsonArrayAttribute can also be added to the type to force it to
    deserialize from a JSON array.</p>
  </blockquote>
  
  <p>My JSON response is like this:</p>
  
  <pre><code>[  {
    "id": 32,
    "consultancy_id": 1,
    "nif": "B61053922",
    "contactname": "",
    "email": "",
    "phone": "",
    "active": true,
    "description": "Keylab"   },   
  {
    "id": 19,
    "consultancy_id": 1,
    "nif": "P0818300F",
    "contactname": "Pau Lloret",
    "email": "lloret@citcea.upc.edu",
    "phone": "",
    "active": true,
    "description": "Rubi"   } ]
  </code></pre>
  
  <p>And those are the classes:</p>
  
  <pre><code>namespace APIEffilogics
  {
      public class Usuari
      {
          public string access_token;    //Encapsulat que conté la identificació de seguretat
          public string token_type;      //Tipus de token, "Bearer"
          public string response;        //Resposta de l'API
  
          public class Client : Usuari    //Estructura client
          {
              [JsonProperty("id")]
              public string cid { get; set; }
              [JsonProperty("consultancy_id")]
              public string consultancy_id { get; set; }
              [JsonProperty("contactname")]
              public string contactname { get; set; }
              [JsonProperty("email")]
              public string email { get; set; }
              [JsonProperty("description")]
              public string description { get; set; }
              [JsonProperty("nif")]
              public string nif { get; set; }
              [JsonProperty("phone")]
              public string phone { get; set; }
              [JsonProperty("active")]
              public string active { get; set; }
          }
          public class Building : Usuari  //Estructura edifici
          {
              public string descrip;
              public string bid;
          }
          public class Floor : Usuari     //Estructura planta
          {
              public string descrip;
              public string fid;
          }
          public class Room : Usuari      //Estructura habitació
          {
              public string descrip;
              public string rid;
          }
          public class Node : Usuari      //Estructura nodes
          {
              public string[] descrip;
              public string[] nid;
              public string[] model;
              public string[] type;
          }
      }
   //************************END PUBLIC CLASS Usuari***************************//
  }
  </code></pre>
  
  <p>The code that I use:</p>
  
  <pre><code>public void Request(string url, string metode)
  {
     try
     {
        //Enviem la petició a la URL especificada i configurem el tipus de connexió
        HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
  
        myReq.KeepAlive = true;
        myReq.Headers.Set("Cache-Control", "no-store");
        myReq.Headers.Set("Pragma", "no-cache");
        myReq.Headers.Set("Authorization", usuari.token_type + " " + usuari.access_token);
  
        if (metode.Equals("GET") || metode.Equals("POST"))
        {
             myReq.Method = metode;  // Set the Method property of the request to POST or GET.
             if (body == true)
             {
                 // add request body with chat search filters
                List&lt;paramet&gt; p = new List&lt;paramet&gt;();
                paramet p1 = new paramet();
                p1.value = "1";
                string jsonBody = JsonConvert.SerializeObject(p1);
                var requestBody = Encoding.UTF8.GetBytes(jsonBody);
                myReq.ContentLength = requestBody.Length;
                myReq.ContentType = "application/json";
                using (var stream = myReq.GetRequestStream())
                {
                    stream.Write(requestBody, 0, requestBody.Length);
                }
                body = false;
             }
        }
        else throw new Exception("Invalid Method Type");
  
        //Obtenim la resposta del servidor
        HttpWebResponse myResponse = (HttpWebResponse)myReq.GetResponse();
        Stream rebut = myResponse.GetResponseStream();
        StreamReader readStream = new StreamReader(rebut, Encoding.UTF8); // Pipes the stream to a higher level stream reader with the required encoding format. 
        string info = readStream.ReadToEnd();
        var jsondata = JsonConvert.DeserializeObject&lt;Usuari.Client&gt;(info);
  
         myResponse.Close();
         readStream.Close();*/
      }
      catch (WebException ex)
      {
         // same as normal response, get error response
         var errorResponse = (HttpWebResponse)ex.Response;
         string errorResponseJson;
         var statusCode = errorResponse.StatusCode;
         var errorIdFromHeader = errorResponse.GetResponseHeader("Error-Id");
         using (var responseStream = new StreamReader(errorResponse.GetResponseStream()))
         {
             errorResponseJson = responseStream.ReadToEnd();
         }
       }
  }
  </code></pre>
  
  <p>I don't know where is the problem, the response have the correct JSON scheme. Somebody can explain me where is the problem in my code, or if I'm not doing correctly.</p>
  
  <p>I've solved the problem how you say, nut now I've a similar problem and the same message error. The JSON response now is this:
  {</p>
  
  <pre><code>    nodes: [
        {
          id: 5,
          global_id: 5,
          description: "Oven",
          room_id: 2,
          floor_id: 1,
          building_id: 1,
          client_id: 2,
          nodemodel_id: 2,
          nodetype_id: 1
        },
        {
          id: 39,
          global_id: 39,
          description: "Fridge",
          room_id: 2,
          floor_id: 1,
          building_id: 1,
          client_id: 2,
          nodemodel_id: 8,
          nodetype_id: 1
        }, ...
      ],
      limit: 10,
      offset: 0
  }
  </code></pre>
  
  <p>And those are the classes:</p>
  
  <pre><code>public class Node : Usuari      //Estructura nodes
  {            
     [JsonProperty("limit")]
     public int limit { get; set; }
     [JsonProperty("offset")]
     public int offset { get; set; }
     [JsonProperty("nodes")]
     public List&lt;Node_sub&gt; nodes_sub { get; set; }
  }
  public class Node_sub : Node
  {
      [JsonProperty("id")]
      public string nid { get; set; }
      [JsonProperty("global_id")]
      public string gid { get; set; }
      [JsonProperty("description")]
      public string descrip { get; set; }
      [JsonProperty("room_id")]
      public string rid { get; set; }
      [JsonProperty("floor_id")]
      public string fid { get; set; }
      [JsonProperty("client_id")]
      public string cid { get; set; }
      [JsonProperty("building_id")]
      public string bid { get; set; }
      [JsonProperty("nodemodel_id")]
      public string model { get; set; }
      [JsonProperty("nodetype_id")]
      public string type { get; set; }
  }
  </code></pre>
  
  <p>The code is the same as before, only I add this sentences:</p>
  
  <pre><code>jsonnode = JsonConvert.DeserializeObject&lt;List&lt;Usuari.Node&gt;&gt;(info);
  </code></pre>
  
  <p>Why I'm having the same error? <code>List&lt;Usuari.Node&gt;</code> is an array that contains all the items of JSON message.</p>
  
  <p>Thanks</p>