JSON.NET反序列化我有下一个JSON回调:{"id":"1","jsonrpc":"2.0","result":{"articles":[{"date":1367582340000,"id":6917,"title":"SomeTitle","author":"NameSurname","event_date":1367584560000,"text":"blabla"}]}}我想为了使用JSON.NET反序列化它,我编写了下一个代码:classNews{privatestringjsonrpc;私有字符串标识;私人结果结果;[JsonProperty("jsonrpc")]publicstringJsonrpc{get{returnjsonrpc;}设置{jsonrpc=值;}}[JsonProperty("id")]publicstringId{get{returnid;}设置{id=值;}}[JsonProperty("result")]publicResultResult{get{returnresult;}设置{结果=值;}}}publicclassResult{公共列表文章;[JsonProperty("articles")]publicListArticles{get{returnarticles;}设置{文章=值;}}}publicclassArticle{公共字符串文本;公共intid;公开日期;公共字符串标题;公共字符串作者;公共字符串imageURL;[JsonProperty("text")]publicstringText{get{returntext;}set{text=value;}}[JsonProperty("id")]publicintId{get{returnid;}set{id=value;}}[JsonProperty("date")]publicintDate{get{returndate;}set{date=value;}}[JsonProperty("title")]publicstringTitle{get{returntitle;}set{title=value;}}[JsonProperty("author")]publicstringAuthor{get{returnauthor;}set{author=value;}}[JsonProperty("imageURL")]publicstringImageURL{get{returnimageURL;}set{imageURL=value;}}}用法:stringJSON=reader.ReadToEnd();Newsent=JsonConvert.DeserializeObject(JSON)asNews;错误:Newtonsoft.Json.DLL中出现“Newtonsoft.Json.JsonSerializationException”类型的异常,但没有用户代码中的问题处理是什么?名为“article”的成员已存在于“results”上使用JsonPropertyAttribute指定不同的名称。将articles字段设为私有:privateListarticles;Name“text”已存在于“Article”成员上的。使用JsonPropertyAttribute指定另一个名称。私有字符串文本;私有intid;私人日期;私有字符串标题;私有字符串作者;私有字符串imageURL;]publicstringText{get;set;}算术运算溢出。让Date很长:[JsonProperty("date")]publiclongDate{get;set;}接下来是我的整个工作类:classNews{[JsonProperty("jsonrpc")]publicstringJsonrpc{get;set;}[JsonProperty("id")]publicstringId{get;放;}[JsonProperty("result")]publicResultResult{get;放;}}publicclassResult{privateListarticles=newList();[JsonProperty("articles")]publicListArticles{get{returnarticles;}}}publicclassArticle{[JsonProperty("text")]publicstringText{get;set;}[JsonProperty("id")]publicintId{get;set;}[JsonProperty("date")]public长日期{get;set;}[JsonProperty("title")]publicstringTitle{get;set;}[JsonProperty("author")]publicstringAuthor{get;放;}[JsonProperty("imageURL")]publicstringImageURL{get;放;请注意,如果您只是反序列化,您甚至不需要JsonProperty-因为除了case之外名称是相同的,所以只有当您同时进行序列化时才需要。以上就是C#学习教程:JSON.NET反序列化分享的全部内容。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
