Take for example the ForumsList method of the Geekzone API, which will (oddly enough) return the list of available forums, and their associated details such as a description, topic and post counts, etc.
Lets mock up how we would consume this service call. First, we'll create a local class structure in our project - into which we are able to map the results we expect back from our API call. In this case we'll create a 'Forum' class. We will be serializing our JSON results into this class, giving us a nice, friendly, strongly typed interface to work against (we like those).
Next we'll add a reference to the JSON.NET project (in this case I'm using Nuget to do so).
Then, using the WebClient class of the .NET Framework, we initiate an asynchronous HTTP GET request to the API URL, which will result in a JSON response string being returned.
In this case, what we have returned is a JSON string representing a collection of forum results. You'll also see within the structure that there are fields which match up perfectly with our 'Forum' class we created earlier (what a co-incidence). This convention means we'll easily be able to auto-magically map these strings into our strongly typed class structure.
Now using the JSON.NET converter class to serialize our string of textual information into a class structure we are left with a 1 to 1 mapped object which retains all hierarchy and structure ready to be used.
So there we go, a couple of small method calls and a little help from our friend JSON.NET and we're consuming a REST API and mapping results to nice friendly objects.
PS. Lookout for additions to the Geekzone API coming soon (including better documentation!).
Download a trial of Visual Studio.
About the author
Phil Gale is a Software Architect and Graphic Designer with over a decade of hands on experience in the web and software industry. He is a director at Red Jungle where they make fantastic software with exceptional user experiences. You can catch him twitter at @redjungle and on Geekzone as well.
Other related posts:
Secure Development Tips and Techniques for use in Asp.net websites and web applications