title: Creating C# object from JSON data retrieved from HttpWebResponse
body: <p>First, I'm absolutely terrible at programming. I always have been. The simplest, most trivial things that any other human being in the world seems to just 'get' tend to trip me up and lead me to uncontrollable rage quitting. That out of the way, sorry for the stupid question. :P</p>
  
  <p>I'm basically trying to take some JSON values returned from an HttpWebResponse and put them into C# object. The JSON values pertain to a list of clients. I want to be able to turn them into C# objects that I can then use throughout my program (e.x. client.name, client.phone_number, etc...).</p>
  
  <p>The code below is more of a proof of concept than final code. I login to a WordPress site and then pull JSON data from a page on the site. I'm able to retrieve and see the returned JSON data (as one long jumbled string). I just can't figure out how to extract from that string the particular values I need to create 'client' objects from them.</p>
  
  <pre><code>        string loginUri = "URL TO WORDPRESS LOGIN";
          string username = entryUsername;
          string password = entryPassword;
          string reqString = "log=" + username + "&amp;pwd=" + password;
          byte[] requestData = Encoding.UTF8.GetBytes(reqString);
  
          CookieContainer loginCookie = new CookieContainer();
          var request = (HttpWebRequest)WebRequest.Create(loginUri);
          request.Proxy = null;
          request.AllowAutoRedirect = false;
          request.CookieContainer = loginCookie;
          request.Method = "POST";
  
          request.ContentType = "application/x-www-form-urlencoded";
          request.ContentLength = requestData.Length;
          using (Stream s = request.GetRequestStream())
          {
              s.Write(requestData, 0, requestData.Length);
  
              using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
              {
                  foreach (Cookie currentCookie in response.Cookies)
                  {
                      Console.WriteLine(currentCookie.Name + " = " + currentCookie.Value); // debug purposes
                      request.CookieContainer.Add(currentCookie);
                  }
              }
          }
  
          // Debug purposes
          Console.WriteLine("{0} cookie(s) have been downloaded, Jim.", loginCookie.Count.ToString());
  
          string clientListURI = "PAGE CONTAINING JSON";
          HttpWebRequest clientListRequest = (HttpWebRequest)WebRequest.Create(clientListURI);
          clientListRequest.Proxy = null;
          clientListRequest.Method = "POST";
          clientListRequest.ContentLength = 0;
          clientListRequest.ContentType = "application/json";
          clientListRequest.CookieContainer = loginCookie;
          using (HttpWebResponse clientListResponse = (HttpWebResponse)clientListRequest.GetResponse())
          using (Stream resSteam = clientListResponse.GetResponseStream())
          {
              JsonValue jsonDoc = JsonObject.Load(resSteam);
              Console.WriteLine("Response: {0}", jsonDoc.ToString ());
          }          
  </code></pre>
  
  <p>Sample Json:</p>
  
  <pre><code>{
      id: "26",
      owner: "7",
      owner_name: "Bradford Kolumbic",
      Status: "0",
      status_name: "None",
      Milestone: "0",
      milestone_name: "None",
      small_text_1: "Ghost Buster Solutions",
      small_text_8: "Business",
      small_text_7: "(858) 123-5432",
      small_text_2: "123 ABC Street",
      small_text_4: "Escondido",
      small_text_5: "California",
      small_text_6: "92027"
  }
  </code></pre>
  
  <p>I created a class, Client, that can be used to store each client profile from the returned JSON.</p>
  
  <pre><code>public class Client
  {
      public string id { get; set; }
      public string owner { get; set; }
      public string owner_name { get; set; }
      public string Status { get; set; }
      public string status_name { get; set; }
      public string Milestone { get; set; }
      public string milestone_name { get; set; }
      public string small_text_1 { get; set; }
      public string small_text_8 { get; set; }
      public string small_text_7 { get; set; }
      public string small_text_2 { get; set; }
      public string small_text_4 { get; set; }
      public string small_text_5 { get; set; }
      public string small_text_6 { get; set; }
  }
  </code></pre>
  
  <p>I've tried all sorts of different ways but nothing seems to work right for me. I'm sure I'm missing something ridiculously obvious.  </p>
  
  <p>The last thing I've tried is:</p>
  
  <pre><code>var JsonClientList = JsonConvert.DeserializeObject&lt;List&lt;Client&gt;&gt;(jsonDoc); 
  </code></pre>
  
  <p>It errors out though with "Unable to cast object of type 'System.Json.JsonObject' to type 'System.Json.JsonPrimitive'."</p>
  
  <p>Any help for this young fool would be greatly appreciated. Thank you for your time.</p>