Click here to Skip to main content
15,880,364 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
I am trying to call an API which returns the data in JSON format which i need to parse. How to do that in System.Net.Webrequest..
Below is my code

C#
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
           
            request = WebRequest.Create("https://IPAaddress/api/admin/configuration/v1/conference/1/");
             
            request.Credentials = new NetworkCredential("username", "password");
            // Create POST data and convert it to a byte array.
            request.Method = "GET";          
           
                    // Set the ContentType property of the WebRequest.
            request.ContentType = "application/json; charset=utf-8";          
   

            WebResponse response = request.GetResponse();
            // Display the status.
            Console.WriteLine(((HttpWebResponse)response).StatusDescription);
            // Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            
            // Display the content.
            Console.WriteLine(responseFromServer);
            // Clean up the streams.
            reader.Close();
            dataStream.Close();
            response.Close();

Thanks in advance

What I have tried:

C#
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
           
            request = WebRequest.Create("https://IPAaddress/api/admin/configuration/v1/conference/1/");
             
            request.Credentials = new NetworkCredential("username", "password");
            // Create POST data and convert it to a byte array.
            request.Method = "GET";          
           
                    // Set the ContentType property of the WebRequest.
            request.ContentType = "application/json; charset=utf-8";          
   

            WebResponse response = request.GetResponse();
            // Display the status.
            Console.WriteLine(((HttpWebResponse)response).StatusDescription);
            // Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            
            // Display the content.
            Console.WriteLine(responseFromServer);
            // Clean up the streams.
            reader.Close();
            dataStream.Close();
            response.Close();
Posted
Updated 22-Sep-16 19:37pm
v2
Comments
F-ES Sitecore 8-Sep-16 4:24am    
Google "parse json c#" and you'll find many examples of how to do this. Please do basic research before asking a question.
Karthik_Mahalingam 9-Sep-16 1:26am    
post sample json data
ankitsrist 9-Sep-16 2:14am    
Here it is karthik
{
"aliases": [
{
"alias": "test",
"conference": "/api/admin/configuration/v1/conference/1/",
"description": "test",
"id": 1
}
],
"allow_guests": true,
"automatic_participants": [],
"call_type": "video",
"description": "",
"force_presenter_into_main": false,
"guest_pin": "",
"guest_view": null,
"host_view": "one_main_seven_pips",
"id": 1,
"ivr_theme": null,
"match_string": null,
"max_callrate_in": null,
"max_callrate_out": null,
"mssip_proxy": null,
"mute_all_guests": false,
"name": "VMR_1",
"participant_limit": null,
"pin": "12345",
"replace_string": "",
"resource_uri": "/api/admin/configuration/v1/conference/1/",
"service_type": "conference",
"sync_tag": "",
"system_location": null,
"tag": ""
}
Karthik_Mahalingam 9-Sep-16 2:15am    
ok, what is your required output
ankitsrist 12-Sep-16 6:08am    
It is giving my required json output, but what i actually want is to use json field to display in my aspx page accordingly for eg. Conferencelbl.text= "name"(field of returned json), can i do something like that. Thanks

1 solution

Create a model and the parse the json object to that model using newtonsoft.json or System.Web.Script.Serialization.

for example:
Model
C#
public class MyModel
  {
      public string user_id { get; set; }
      public string name { get; set; }
      public string given_name { get; set; }
      public string email { get; set; }    
  }


and your response

C#
WebResponse response = request.GetResponse();
           // Display the status.
           Console.WriteLine(((HttpWebResponse)response).StatusDescription);
           // Get the stream containing content returned by the server.
           dataStream = response.GetResponseStream();
           // Open the stream using a StreamReader for easy access.
           StreamReader reader = new StreamReader(dataStream);
           // Read the content.
           string responseFromServer = reader.ReadToEnd();
// im using javascriptserializer from the System.Web.Script.Serialization namespace
          JavaScriptSerializer j = new JavaScriptSerializer();
          MyModel model= j.Deserialize<MyModel>(responseFromServer .Content);


Hope this helps..
 
Share this answer
 
Comments
ankitsrist 12-Sep-16 8:21am    
thanks sunil, by doing slight change in your code my problem got solved
string responseFromServer = reader.ReadToEnd();
JavaScriptSerializer js = new JavaScriptSerializer();
dynamic obj = js.Deserialize

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900