My blog has moved!

Visit http://blog.adamroderick.com/ and update your bookmarks.

Friday, November 6, 2009

Google Search API with C# and JSON.NET

Google exposes a free search API.  Of course, they do not provide a lot of support for Microsoft developers—they do have a .NET library available, but it is for .NET 1.1, which is several versions behind. 

I found that they have a REST API available, and the basic search API does not even require a developer token.  Connecting is straightforward enough:

image

In the first line, you can see by the end of the string that I am searching for “Monster Truck.”  Then we use the WebClient class to download the result into a string.  The results look similar to this.

That’s JSON.  I really haven’t spent much time working with JSON in .NET, but there are several libraries available to help with it.  I chose to look at JSON.NET.  Its basic functionality is to take JSON and parse it into a hierarchy of dictionary objects.  Here is how I can loop over the results of the Google search query:

image

JObject.Parse takes a string and parses the JSON.  This allows navigating the hierarchy using lines like o[“responseData”][“results”].  More information on the classes available in JSON.NET is available here.

Labels: , , ,

1 Comments:

Blogger Georg Zimmer said...

nice, thanks

November 20, 2009 at 3:29 PM  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home