C# 如何在System.Net.Webrequest响应中解析JSON数据

C# 如何在System.Net.Webrequest响应中解析JSON数据,c#,rest,api,webrequest,webresponse,C#,Rest,Api,Webrequest,Webresponse,我试图调用一个API,它以JSON格式返回我需要解析的数据。如何在System.Net.Webrequest中执行此操作。。 下面是我的代码 ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications); request = WebRequest.C

我试图调用一个API,它以JSON格式返回我需要解析的数据。如何在System.Net.Webrequest中执行此操作。。 下面是我的代码

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();

Webrequest只返回来自远程资源的响应。您需要自己解析JSON,就像使用DataContractJsonSerializer或JSON.Net()

谢谢大家我的问题解决了,下面是代码

ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);

        request = WebRequest.Create("https://ipaddress/api/admin/configuration/v1/conference/1/");

        request.Credentials = new NetworkCredential("admin", "admin123");
        // 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();
        JavaScriptSerializer js = new JavaScriptSerializer();
        var obj = js.Deserialize<dynamic>(responseFromServer);
        Label1.Text = obj["name"];
        // Display the content.
        Console.WriteLine(responseFromServer);
        // Clean up the streams.
        reader.Close();
        dataStream.Close();
        response.Close();
ServicePointManager.ServerCertificateValidationCallback=新系统.Net.Security.RemoteCertificateValidationCallback(接受认证);
request=WebRequest.Create(“https://ipaddress/api/admin/configuration/v1/conference/1/");
request.Credentials=newnetworkcredential(“admin”、“admin123”);
//创建POST数据并将其转换为字节数组。
request.Method=“GET”;
//设置WebRequest的ContentType属性。
request.ContentType=“application/json;charset=utf-8”;
WebResponse=request.GetResponse();
//显示状态。
Console.WriteLine(((HttpWebResponse)response.StatusDescription);
//获取包含服务器返回的内容的流。
dataStream=response.GetResponseStream();
//使用StreamReader打开流以便于访问。
StreamReader=新的StreamReader(数据流);
//阅读内容。
字符串responseFromServer=reader.ReadToEnd();
JavaScriptSerializer js=新的JavaScriptSerializer();
var obj=js.反序列化(responseFromServer);
Label1.Text=obj[“name”];
//显示内容。
Console.WriteLine(responseFromServer);
//清理溪流。
reader.Close();
dataStream.Close();
response.Close();

也许这个答案会对您有所帮助:看看我关于CodeProject的文章:谢谢这篇文章很有帮助。我需要创建数据协定类吗?最简单的方法是在项目中包含Newtonsoft包:在package Manager控制台中,选择项目,然后运行
安装包Newtonsoft.Json
。当您想要强制执行契约时,使用数据契约类型是合适的,这可能不是您想要的。