JSON.NET Deserealization
我有下一个JSON回调:
{ "id":"1", "jsonrpc":"2.0", "result":{ "articles":[ { "date":1367582340000, "id":6917, "title":"Some Title", "author":"Name Surname", "event_date":1367584560000, "text":"blabla" } ] } }
我想使用JSON.NET反序列化它
我写了下一个代码:
class News { private string jsonrpc; private string id; private Result result; [JsonProperty("jsonrpc")] public string Jsonrpc { get { return jsonrpc; } set { jsonrpc = value; } } [JsonProperty("id")] public string Id { get { return id; } set { id = value; } } [JsonProperty("result")] public Result Result { get { return result; } set { result = value; } } } public class Result { public List articles; [JsonProperty("articles")] public List Articles { get { return articles; } set { articles = value; } } } public class Article { public string text; public int id; public int date; public string title; public string author; public string imageURL; [JsonProperty("text")] public string Text { get { return text; } set { text = value; } } [JsonProperty("id")] public int Id { get { return id; } set { id = value; } } [JsonProperty("date")] public int Date { get { return date; } set { date = value; } } [JsonProperty("title")] public string Title { get { return title; } set { title = value; } } [JsonProperty("author")] public string Author { get { return author; } set { author = value; } } [JsonProperty("imageURL")] public string ImageURL { get { return imageURL; } set { imageURL = value; } } }
用法:
string JSON = reader.ReadToEnd(); News ent = JsonConvert.DeserializeObject(JSON) as News;
落错了:
Newtonsoft.Json.DLL中出现“Newtonsoft.Json.JsonSerializationException”类型的exception,但未在用户代码中处理
问题是什么?
“结果”上已存在名称为“文章”的成员。 使用JsonPropertyAttribute指定其他名称。
将articles
字段设为private
:
private List articles;
“Article”上已存在名为“text”的成员。 使用JsonPropertyAttribute指定其他名称。
又是老样子:
private string text; private int id; private int date; private string title; private string author; private string imageURL;
或更好 – 使用自动实现的属性并丢弃字段,即
[JsonProperty("text")] public string Text {get;set;}
算术运算导致溢出。
让Date
很long
:
[JsonProperty("date")] public long Date {get;set;}
接下来是我的整个工作class:
class News { [JsonProperty("jsonrpc")] public string Jsonrpc {get;set;} [JsonProperty("id")] public string Id { get; set; } [JsonProperty("result")] public Result Result { get; set; } } public class Result { private List articles = new List (); [JsonProperty("articles")] public List Articles { get { return articles; }} } public class Article { [JsonProperty("text")] public string Text {get;set;} [JsonProperty("id")] public int Id {get;set;} [JsonProperty("date")] public long Date {get;set;} [JsonProperty("title")] public string Title {get;set;} [JsonProperty("author")] public string Author { get; set; } [JsonProperty("imageURL")] public string ImageURL { get; set; } }
请注意,如果您只是反序列化,甚至不需要JsonProperty
– 因为名称除了大小写外都是相同的,所以只有在您同时进行序列化时才会这样。
上述就是C#学习教程:JSON.NET Deserealization分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1250942.html