Convert response from rest WCF service to class object using C# in ASP.Net

tex
 
on Mar 01, 2021 12:57 AM
782 Views

I have rest WCF service link and i create integration with it and made the call but i want to cast the response of the call to class object using c#

This response in JSON 

{"Response":{"Code":-27,"ErrorMessage":"Please call login first.","IsSuccess":false,"SuccessMessage":""},"AllBlocked":false,"Consents":null,"Total":0}

 

public class GetConsentResponse
{
    public int Total { get; set; }
    public bool AllBlocked { get; set; }
    public List<Consent> Consents { get; set; }
}
 
public class Consent
{
     public string ConsentID { get; set; }
     public bool Blocked { get; set; }
     public string Type { get; set; }
}

 

                HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(new Uri(serviceUri)); 
                httpRequest.Accept = "application/json"; 
                httpRequest.ContentType = "application/json";
                httpRequest.Method = "GET"; 
                using (HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse())
                {
                    using (Stream stream = httpResponse.GetResponseStream())
                    {
                        GetConsentResponse response = new GetConsentResponse();
 
                        richTextBox1.Text = (new StreamReader(stream)).ReadToEnd();
                    }
                }

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
tex
 
on Mar 01, 2021 04:32 AM
on Mar 01, 2021 04:35 AM

Thank u dharmendr

I have fix my issue and this the solution.

string json = (new StreamReader(stream)).ReadToEnd();

JavaScriptSerializer serializer = new JavaScriptSerializer();

response = (GetConsentResponse)serializer.Deserialize(json, typeof(GetConsentResponse));

Make sure that name in json string same with object property.

This website to convert JSON string to class 

https://json2csharp.com/