JSON反序列化对象HTTPWebResponse

编程入门 行业动态 更新时间:2024-10-25 22:34:51
本文介绍了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":"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);

提琴: dotnetfiddle/2xR7hO

更多推荐

JSON反序列化对象HTTPWebResponse

本文发布于:2023-11-26 17:31:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634518.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   序列化   JSON   HTTPWebResponse

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!