
所以我已经能够获得一些JsON对象,但是这个对象比较复杂。
我试图从Reddit获得意见。 这里是我使用的方法:
public async Task<List<string>> GetComments(string currentSubreddit,string topicID) { string commentUrl = "http://www.reddit.com/r/" + currentSubreddit + "/comments/" + topicID + "/.Json"; List<Comments> commentList = new List<Comments>(); string JsonText = await wc.GetJsonText(commentUrl); Comments.RootObject deserializeObject = Newtonsoft.Json.JsonConvert.DeserializeObject<Comments.RootObject>(JsonText); List<string> commentListTest = new List<string>(); //List<string> commentListTest = deserializeObject.data.children[0].data.children; return commentListTest; }
这是GetJsonText方法:
public async Task<string> GetJsonText(string url) { var request = WebRequest.Create(url); string text; request.ContentType = "application/Json; charset=utf-8"; var response = (httpWebResponse)await request.GetResponseAsync(); using (var sr = new StreamReader(response.GetResponseStream())) { text = sr.ReadToEnd(); } return text; }
这里是对象的链接: http ://pastebin.com/WQ8XXGNA并链接到JsonText: http ://pastebin.com/7Kh6cA9a
我如何序列化System.Security.AccessControl.fileSecurity?
如何序列化一个对象通过networking发送
在文件描述符上使用fwrite /将文件描述符转换为文件指针
linux上的C ++串口。 在串口上可以同时读写吗?
用于linux的C ++对象序列化
返回的错误说:
mscorlib.dll中发生types'Newtonsoft.Json.JsonSerializationException'的exception,但未在用户代码中处理
附加信息:由于types需要JsON对象(例如{“name”:“value”})来正确地反序列化,因此无法反序列化当前JsON数组(例如[1,2,3])到types“JuicyReddit.Comments + RootObject” 。
如果有人能帮我弄清楚这是什么错误,我会很感激。 谢谢
如何在C ++中使用inheritance对类进行序列化
在实现vIDeo游戏的基本二进制序列化时,可移植性是否需要担心?
通过序列化支持的单例类
将文件元数据存储在额外的文件中
从未知文件中提取序列化的数据
实际上你的代码有几个问题
public async Task<List<string>> GetComments(string currentSubreddit,string topicID)
你不需要在这里返回一个字符串列表,你需要返回一个完整的对象
首先将模型中的RootObject重命名为适当的名称,例如“CommentsObject”
所以像这样设置你的类,并将其命名为CommentsObject.cs:
using System; using System.Collections.Generic; using System.linq; using System.Text; using System.Threading.Tasks; namespace YOURnameSPACE.Comments { public class MediaEmbed { } public class SecureMediaEmbed { } public class Data4 { public int count { get; set; } public string parent_ID { get; set; } public List<string> children { get; set; } public string name { get; set; } public string ID { get; set; } public string subreddit_ID { get; set; } public object banned_by { get; set; } public string subreddit { get; set; } public object likes { get; set; } public object replIEs { get; set; } public bool? saved { get; set; } public int? gilded { get; set; } public string author { get; set; } public object approved_by { get; set; } public string body { get; set; } public object edited { get; set; } public object author_flair_CSS_class { get; set; } public int? downs { get; set; } public string body_HTML { get; set; } public string link_ID { get; set; } public bool? score_hIDden { get; set; } public double? created { get; set; } public object author_flair_text { get; set; } public double? created_utc { get; set; } public object distinguished { get; set; } public object num_reports { get; set; } public int? ups { get; set; } } public class Child2 { public string kind { get; set; } public Data4 data { get; set; } } public class Data3 { public string modhash { get; set; } public List<Child2> children { get; set; } public object after { get; set; } public object before { get; set; } } public class ReplIEs { public string kind { get; set; } public Data3 data { get; set; } } public class Data2 { public string domain { get; set; } public object banned_by { get; set; } public MediaEmbed media_embed { get; set; } public string subreddit { get; set; } public object selftext_HTML { get; set; } public string selftext { get; set; } public object likes { get; set; } public object secure_media { get; set; } public object link_flair_text { get; set; } public string ID { get; set; } public SecureMediaEmbed secure_media_embed { get; set; } public bool clicked { get; set; } public bool stickIEd { get; set; } public string author { get; set; } public object media { get; set; } public int score { get; set; } public object approved_by { get; set; } public bool over_18 { get; set; } public bool hIDden { get; set; } public string thumbnail { get; set; } public string subreddit_ID { get; set; } public object edited { get; set; } public object link_flair_CSS_class { get; set; } public object author_flair_CSS_class { get; set; } public int downs { get; set; } public bool saved { get; set; } public bool is_self { get; set; } public string permalink { get; set; } public string name { get; set; } public double created { get; set; } public string url { get; set; } public object author_flair_text { get; set; } public string Title { get; set; } public double created_utc { get; set; } public int ups { get; set; } public int num_comments { get; set; } public bool visited { get; set; } public object num_reports { get; set; } public object distinguished { get; set; } public ReplIEs replIEs { get; set; } public int? gilded { get; set; } public string parent_ID { get; set; } public string body { get; set; } public string body_HTML { get; set; } public string link_ID { get; set; } public bool? score_hIDden { get; set; } public int? count { get; set; } public List<string> children { get; set; } } public class Child { public string kind { get; set; } public Data2 data { get; set; } } public class Data { public string modhash { get; set; } public List<Child> children { get; set; } public object after { get; set; } public object before { get; set; } } public class CommentsObject { public string kind { get; set; } public Data data { get; set; } } }
让你的名字空间正确!
然后处理请求和反序列化到注释对象的列表中:(如果需要,可以使用webclIEnt而不是httpclIEnt,这只是一个例子)
private httpClIEnt clIEnt; public async Task<List<CommentsObject>> GetComments() { clIEnt = new httpClIEnt(); var response = await clIEnt.GetAsync("http://www.reddit.com/r/AskReddit/comments/1ut6xc.Json"); if (response.IsSuccessstatusCode) { string Json = await response.Content.ReadAsstringAsync(); List<CommentsObject> comments = await JsonConvert.DeserializeObjectAsync<List<CommentsObject>>(Json); return comments; } else { throw new Exception("Errorhandling message"); } }
这不是理想的(不是完全的答案,而是更多的解决方法),但是我创建了模拟reddit响应Json的模型,使得反序列化非常容易。 我在我的模型属性上使用JsonProperty属性来使模型稍微有点儿。
这里是模型
由于我的模型直接模拟JsON,我只能使用Json.net的通用反序列化方法。
总结以上是内存溢出为你收集整理的C#JSON对象不会反序列化全部内容,希望文章能够帮你解决C#JSON对象不会反序列化所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)