Skip Navigation LinksHome > Categories > Code from a Category

Using the Google AJAX Search API: JSON serialisation with .NET



User Name: codelecturer
Name: Mike Chauhan
Contact Me: www.datawebcoder.com/ContactUs.aspx
Home Page: www.datawebcoder.com
6 years of total IT experience including programming, application development, System Maintenance . 3 years of Experienced and expertise in .Net Framework VB, Visual Basic.Net, ASP.Net 2.0, 3.5 and AD... [More]
Viewed Times: 199
Add Date: 01/08/2012
Google’s AJAX Search API is designed to let you add searches to web pages via JavaScript. However, there’s nothing to stop you using .NET to execute searches on the server side.


There’s a certain cleanliness to being able to do everything on the server, and I certainly prefer not to have to do all the work in the browser. This article explains how to use the Google search API in .NET

Using the Google Search API

The API exposes a simple RESTful interface and the URLs and arguments for the different searches are documented in Google’s API reference.

The API itself is functionally limited as it is optimised to serve JavaScript-based clients. There are also a number of restrictions on what you can do with the results of a search call. You have to display results as they are delivered – no splitting, re-sorting or tweaking – and you also have to clearly state that Google is the source of the results. You’re also obliged to provide the url of the referring page when you make the API call. It’s worth having a look at Google’s terms of use to ensure that your application meets the requirements.

Working with the API is simple enough, so long as you are comfortable with working with Json in .NET.

Json serialization in .NET

WCF provides support for serializing and deserializing Json via the DataContractJsonSerializer class in the System.Runtime.Serialization.Json namespace.

The code structure is very similar as XML Serialization. Firstly, you have to create a set of data classes that reflect the structure of the Json output. The classes have to be decorated with the [DataContract] attribute and each member has to be decorated with a [DataMember] attribute.

The following is a simple example of some Json output:



This will require two data classes – one to hold the results and another to describe each result:



The code example is a generic deserialisation method that can be applied to any Json deserialisation:



The deserialisation method would be called as follows:



Post a Comment

Name: (Optional)
Email: (Optional, you can get an email if somebody replys your comments)*
Email me if somebody respons my comment below:
Details**:
Enter Text
as Below:
(case insensitive, if hard to read, click the "get a new one" button)
 
    
* Your email address will not be shared with any third parties for any reason.
** Maximum 1000 charactors.