Thursday, July 4, 2013

Get Json Data from a URL



public string Getjsondata(string URL)
    {
        System.Net.HttpWebRequest request = null;
        StreamReader responseReader = null;
        string responseData = "";
        try
        {
            request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL);
            responseReader = new StreamReader(request.GetResponse().GetResponseStream());
            responseData = responseReader.ReadToEnd();
        }
        catch
        {
            throw;
        }
        finally
        {
            request.GetResponse().GetResponseStream().Close();
            responseReader.Close();
            responseReader = null;
        }
        return responseData;
    }
Explanation:
  The above method gets the JSON Data form the Url which is passed to the method. Json Data is usually a string in the format of name and value.

The example of json string 


{ "FirstName":"Madhan","LastName":"Kumar" }


How To Find the json string is correct or valid?
For this there are online tools called json parser in which u can paste the string and you can check whether the json  string is correct.
The favourite json Parsers are
http://jsonlint.com/
http://json.parser.online.fr/

No comments:

Post a Comment