CodeKicks.com
Focus on Microsoft Technologies - Tutorials, Articles, Code Samples.

Tuesday, September 05, 2006

Yahoo! Web Services Request and AjaxPro JSON Parser in .NET

Yesterday night I build an example on how to use Yahoo! Web Services with the AjaxPro JSON parser. The example will call a Yahoo! Web Service with output type set to JSON (see http://developer.yahoo.com/common/json.html). The response will be deserialized to an .NET structure using the AjaxPro JSON parser (from the stand-alone version or the build-in parser in Ajax.NET Professional).

First of all I have to get the response from the Web Service using the WebRequest class:

string query = "Madonna";    // demo search for "Madonna"
int count = 5;

WebRequest request = WebRequest.Create(
   "
http://api.search.yahoo.com/ImageSearchService/V1/imageSearch?appid=YahooDemo=" +
   query + "&results=" + count + "&output=json"
);

request.Credentials = CredentialCache.DefaultCredentials;

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);

string responseFromServer = reader.ReadToEnd();

reader.Close();
dataStream.Close();
response.Close();

Now, I have to deserialize the JSON response string to an .NET data type. I can simply generate an IJavaScriptObject from the JSON string and go to all properties. But today I'd like to get a real .NET class. I had a look on the result XML schema and build my own structures that will represent the results from the Yahoo! Web Service:continue…

Post a Comment