title: Deserializing JSON using C#
body: <p>Finding some difficulty in sourcing information in trying to deserialize JSON in C#.</p>
  
  <p>I have results from Google custom search returned in JSON format. I just want to check through my steps and establish the order in trying to deserialize it. Is this right?</p>
  
  <ol>
  <li>I need to create classes to match
  the JSON format. Kind of like a
  creating schema file. </li>
  <li>Use the <code>JavaScriptSerializer()</code> class and
  <code>deserialize</code> method to extract the
  relevant bits.</li>
  </ol>
  
  <p>One of the issues I think I am going to run into is that I don't require all of the data returned but only the html links. How can I achieve that?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I have updated my question with the following JSON snippet and C# code. I want to output the string 'links' to console but it doesn't seem to be working. I think I am defining my classes wrongly? </p>
  
  <p>JSON from Google Custom Search</p>
  
  <pre><code>handleResponse({
   "kind": "customsearch#search",
   "url": {
    "type": "application/json",
    "template": "https://www.googleapis.com/customsearch/v1?q\u003d{searchTerms}&amp;num\u003d{count?}&amp;start\u003d{startIndex?}&amp;hr\u003d{language?}&amp;safe\u003d{safe?}&amp;cx\u003d{cx?}&amp;cref\u003d{cref?}&amp;sort\u003d{sort?}&amp;alt\u003djson"
   },
   "queries": {
    "nextPage": [
     {
      "title": "Google Custom Search - lectures",
      "totalResults": 9590000,
      "searchTerms": "lectures",
      "count": 1,
      "startIndex": 2,
      "inputEncoding": "utf8",
      "outputEncoding": "utf8",
      "cx": "017576662512468239146:omuauf_lfve"
     }
    ],
    "request": [
     {
      "title": "Google Custom Search - lectures",
      "totalResults": 9590000,
      "searchTerms": "lectures",
      "count": 1,
      "startIndex": 1,
      "inputEncoding": "utf8",
      "outputEncoding": "utf8",
      "cx": "017576662512468239146:omuauf_lfve"
     }
    ]
   },
   "context": {
    "title": "Curriculum",
    "facets": [
     [
      {
       "label": "lectures",
       "anchor": "Lectures"
      }
     ],
     [
      {
       "label": "assignments",
       "anchor": "Assignments"
      }
     ],
     [
      {
       "label": "reference",
       "anchor": "Reference"
      }
     ]
    ]
   },
   "items": [
    {
     "kind": "customsearch#result",
     "title": "EE364a: Lecture Videos",
     "htmlTitle": "EE364a: \u003cb\u003eLecture\u003c/b\u003e Videos",
     "link": "http://www.stanford.edu/class/ee364a/videos.html",
     "displayLink": "www.stanford.edu",
     "snippet": "Apr 7, 2010 ... Course materials. Lecture slides · Lecture videos (2008) · Review sessions.   Assignments. Homework · Reading. Exams. Final exam ...",
     "htmlSnippet": "Apr 7, 2010 \u003cb\u003e...\u003c/b\u003e Course materials. \u003cb\u003eLecture\u003c/b\u003e slides · \u003cb\u003eLecture\u003c/b\u003e videos (2008) · Review sessions. \u003cbr\u003e  Assignments. Homework · Reading. Exams. Final exam \u003cb\u003e...\u003c/b\u003e",
     "cacheid": "TxVqFzFZLOsJ"
    }
   ]
  }
  );
  </code></pre>
  
  <p>C# Snippet</p>
  
  <pre><code>public class GoogleSearchResults
  {
      public string link { get; set; }
  
  }
  
  public class Program
  {
      static void Main(string[] args)
      {
          //input search term
          Console.WriteLine("What is your search query?:");
          string searchTerm = Console.ReadLine();
  
          //concantenate the strings using + symbol to make it URL friendly for google
          string searchTermFormat = searchTerm.Replace(" ", "+");
  
          //create a new instance of Webclient and use DownloadString method from the Webclient class to extract download html
          WebClient client = new WebClient();
          string Json = client.DownloadString("https://www.googleapis.com/customsearch/v1?key=My Key&amp;cx=My CX&amp;q=" + searchTermFormat);
  
          //create a new instance of JavaScriptSerializer and deserialise the desired content
          JavaScriptSerializer js = new JavaScriptSerializer();
          GoogleSearchResults results = js.Deserialize&lt;GoogleSearchResults&gt;(Json);
  
          Console.WriteLine(results);
          //Console.WriteLine(htmlDoc);
          Console.ReadLine();
      }
  }
  </code></pre>
  
  <p>Thanks</p>