如何解析system.net.webrequest响应中的JSON数据 [英] How to parse JSON data in system.net.webrequest reponse
本文介绍了如何解析system.net.webrequest响应中的JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,
我试图调用一个API,它返回我需要解析的JSON格式的数据。如何在System.Net.Webrequest中这样做..
以下是我的代码
ServicePointManager。 ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
request = WebRequest.Create( https:// IPAaddress / api / admin / configuration / V1 /会议/ 1 /跨度>);
request.Credentials = new NetworkCredential( username, password);
// 创建POST数据并将其转换为字节数组。
请求.Method = GET;
// 设置WebRequest的ContentType属性。
request.ContentType = application / json; charset = utf-8;
WebResponse response = request.GetResponse();
// 显示状态。
Console.WriteLine(((HttpWebResponse)响应).StatusDescription);
// 获取包含服务器返回内容的流。
dataStream = response.GetResponseStream();
// 使用StreamReader打开流以便于访问。
StreamReader阅读器= new StreamReader(dataStream);
// 阅读内容。
string responseFromServer = reader.ReadToEnd();
// 显示内容。
Console.WriteLine( responseFromServer);
// 清理流。
reader.Close();
dataStream.Close();
response.Close();
提前致谢
我有什么试过:
ServicePointManager.ServerCertificateValidationCallback = new 系统.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
request = WebRequest.Create( https:// IPAaddress / api / admin / configuration / V1 /会议/ 1 /跨度>);
request.Credentials = new NetworkCredential( username, password);
// 创建POST数据并将其转换为字节数组。
请求.Method = GET;
// 设置WebRequest的ContentType属性。
request.ContentType = application / json; charset = utf-8;
WebResponse response = request.GetResponse();
// 显示状态。
Console.WriteLine(((HttpWebResponse)响应).StatusDescription);
// 获取包含服务器返回内容的流。
dataStream = response.GetResponseStream();
// 使用StreamReader打开流以便于访问。
StreamReader阅读器= new StreamReader(dataStream);
// 阅读内容。
string responseFromServer = reader.ReadToEnd();
// 显示内容。
Console.WriteLine( responseFromServer);
// 清理流。
reader.Close();
dataStream.Close();
response.Close();
解决方案
创建模型并使用newtonsoft.json将json对象解析为该模型或者System.Web.Script.Serialization。
例如:
型号
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 ; }
}
和你的回复
WebResponse response = request.GetResponse();
// 显示状态。
Console.WriteLine(((HttpWebResponse)响应).StatusDescription);
// 获取包含服务器返回内容的流。
dataStream = response.GetResponseStream();
// 使用StreamReader打开流以便于访问。
StreamReader阅读器= new StreamReader(dataStream);
// 阅读内容。
string responseFromServer = reader.ReadToEnd();
// 即时通讯使用System.Web.Script.Serialization命名空间中的javascriptserializer
JavaScriptSerializer j = new JavaScriptSerializer();
MyModel model = j.Deserialize< MyModel>(responseFromServer .Content);
希望这会有所帮助..
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
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:
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();
解决方案
Create a model and the parse the json object to that model using newtonsoft.json or System.Web.Script.Serialization.
for example:
Model
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
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..
这篇关于如何解析system.net.webrequest响应中的JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文