JSON in ASP.NET Ajax: Part 2. Calling a remote JSON Web Service from client script
|
|
|
|
| Articles Reviews ASP | |
| Written by JOHN | |
| Wednesday, 24 October 2007 | |
|
Now, after the very futile example which accompanied the introduction to JavaScript Object Notation in my last post (by the way, I forgot to mention that this is what the fancy acronym stands for, but I'm sure you already knew that), let's go straight to the fun part, and see how we can leverage the flexibility of JSON (and ASP.NET AJAX, of course) to achieve some less trivial result. When developing a web application, we often need to aggregate data from different sources. You might argue this is no big news, as we've all used web services in a way or the other, and "mashup" is surely not an uncommon word these days.
But probably, for most of us, the natural way to think of data returned from a web service call is XML. But XML is not exactly the easiest thing to parse on the client, nor a very succinct format. That's why a number of JSON web services have flourished lately. Probably the first that comes to mind is Google Maps' Geocoder (see here), but there are a lot more. If you are looking for inspiration you can have a look at xmethods. But today, we are focusing on some very, very interesting web methods kindly offered by Geonames. In fact, as I've always mantained that an example is worth a googolplex words, in this post I'm going to describe a specific problem I had to solve recently, and the rather simple solution I found, using JSON and ASP.NET AJAX. So, let's get this started: enter The Problem. Let's imagine we want to retrieve the most recent weather information for a specific location, given its geographic coordinates. In particular, let's imagine that we let the user input the coordinates directly on the client (for example by clicking on a Virtual Earth map) and we want to visualize the latest weather data observed at the nearest weather station, avoiding the user the annoyance of the much dreaded postback. The Solution. Luckily, the findNearByWeatherJSON web service at Geonames does exactly what we need. It takes a longitude and latitude as parameters, and returns a JSON object describing the weather conditions gathered at the nearest observing site. Powered by jReviews |
|
| Last Updated ( Thursday, 03 January 2008 ) | |
| Next > |
|---|







