Sunday, May 11, 2014

How to Get(Download) Json Data from Url in Asp.Net

6 comments
Sometime in asp.net we need to download json data from a url and then store it in a variable. In this example I am going to discus the way to fetch data from a url which return data in json format in your asp.net web application.

System.Net.WebClient class contain the method DownloadString() to download json from a url.

Example showing the code of Downloading json data from URL in Asp.Net using C#:

Add the Following code in Code Behind :

Add Namespace: 
using System.Net;

protected void Page_Load(object sender, EventArgs e)
    {
               downloadjson();
    }

    private void downloadjson()
    {   var w = new WebClient();
        string url = "http://api.worldweatheronline.com/free/v1/weather.ashx?q=London&format=json&num_of_days=5&key=8s678k5fh3gfdta9z742fkme";
        var jsondata = string.Empty;
        jsondata = w.DownloadString(url);
        var result = JsonConvert.DeserializeObject<test>(jsondata);
    }
    public class test

    {
        public Dictionary<object, object> data { get; set; }
     
    }


Sample Output:


6 comments: