.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Javascript >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Working with JSON in ASP.NET

Posted By:Athira Sreejith       Posted Date: February 22, 2009    Points: 10    Category: Javascript    URL: http://www.dotnetspark.com  
 

Working with JSON in ASP.NET Class file: Below is the sample code

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Net;

using System.Globalization;

using System.IO;

using System.Text;

///

/// Summary description for JSONTest

///

public class JSONSearchTest

{

public JSONSearchTest()

{

}

//private readonly static string FindNearbyWeatherUrl

// ="http://ws.geonames.org/findNearByWeatherJSON?lat={0}&lng={1}";

private readonly static string FindbyiTuneUrl

="http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStoreServices.woa/wa/wsSearch?term={0}&country={1}&limit={2}&media={3}";

public static string GetiTuneBySearch(string term,string country,double limit,string media)

{

string formattedUri = String.Format(CultureInfo.InvariantCulture, FindbyiTuneUrl, term, country, limit, media);

HttpWebRequest webRequest = GetWebRequest(formattedUri);

HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();

string jsonResponse = string.Empty;

// Get the stream associated with the response.

Stream receiveStream = response.GetResponseStream();

// Pipes the stream to a higher level stream reader with the required encoding format.

StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);

jsonResponse = readStream.ReadToEnd();

//Console.WriteLine("Response stream received.");

//Console.WriteLine(readStream.ReadToEnd());

response.Close();

readStream.Close();

//using (StreamReader sr = new StreamReader(response.GetResponseStream()))

//{

// jsonResponse = sr.ReadToEnd();

//}

return jsonResponse;

}

private static HttpWebRequest GetWebRequest(string formattedUri)

{

// Create the request's URI.

Uri serviceUri = new Uri(formattedUri, UriKind.Absolute);

// Return the HttpWebRequest.

return (HttpWebRequest)System.Net.WebRequest.Create(serviceUri);

}

}

Now UseĀ in .cs Page:

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

public partial class JSONTest : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

string jsonoutput = string.Empty;

jsonoutput = JSONSearchTest.GetiTuneBySearch("john", "GB", 25, "apps");

Response.Write(jsonoutput);

}

}



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend