JSON反序列化对象HTTPWebResponse [英] JSON Deserialize Object HTTPWebResponse
本文介绍了JSON反序列化对象HTTPWebResponse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试获取我的Web请求JSON输出并进行解析.这是输出
I am trying to take my Web Request JSON output and parse it. Here is the output
{
"kind":"internal",
"name":"SplashPageToggle_dg",
"fullPath":"SplashPageToggle_dg",
"generation":1255326,
"selfLink":"https://link",
"type":"stri ng",
"records":[
{
"name":"enable_app1",
"data":"0"
},
{
"name":"enable_app2",
"da ta":"0"
},
{
"name":"enable_app3",
"data":"0"
},
{
"name":"enable_app4",
"data":"0"
},
{
"name":"enable_app5",
"data":"0"
},
{
"name":"enable_app6",
"data":"1"
},
{
"name":"enable_app7",
"data":"0"
},
{
"name":"enable_app8",
"data":"0"
},
{
"name":"enable_app9",
"data":"0"
},
{
"name":"enable_app10",
"data":"0"
}
]
}
我为这些结果创建了公共课程
I have created public classes for these results
public class RootObject
{
public string kind { get; set; }
public string name { get; set; }
public string fullPath { get; set; }
public int generation { get; set; }
public string selfLink { get; set; }
public string type { get; set; }
public List<Record> records { get; set; }
}
public class Record
{
public string name { get; set; }
public string data { get; set; }
}
当我尝试反序列化Record类并选择名称时,我从RootObject类获得了名称.这是我的代码
When I try to deserialize the Record class and choose the name, I get the name from the RootObject class. Here is my code
static void Main(string[] args)
{
string url = "URL";
HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(url);
getRequest.Method = "GET";
getRequest.Credentials = new NetworkCredential("UN", "PW");
ServicePointManager.ServerCertificateValidationCallback = new
RemoteCertificateValidationCallback
(
delegate { return true; }
);
var getResponse = (HttpWebResponse)getRequest.GetResponse();
Stream newStream = getResponse.GetResponseStream();
StreamReader sr = new StreamReader(newStream);
var result = sr.ReadToEnd();
var splashInfo = JsonConvert.DeserializeObject<Record>(result);
Console.WriteLine(splashInfo.name);
Console.ReadLine();
}
推荐答案
您正尝试将JSON反序列化为错误的类.
You are trying to deserialize your JSON into the wrong class.
更改此行:
var splashInfo = JsonConvert.DeserializeObject<Record>(result);
对此:
var splashInfo = JsonConvert.DeserializeObject<RootObject>(result);
提琴: https://dotnetfiddle.net/2xR7hO
这篇关于JSON反序列化对象HTTPWebResponse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文