且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JSON.NET去实现

更新时间:2023-01-17 19:45:37

结果"上已经存在一个名为文章"的成员.使用JsonPropertyAttribute指定另一个名称.

articles字段设置为private:

private List<Article> articles;

文章"上已经存在一个名为文本"的成员.使用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 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<Article> articles = new List<Article>();
    [JsonProperty("articles")]
    public List<Article> 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-因为名称全都相同(除了大小写相同),只有在您同时进行序列化时,这才有意义. /p>

I have next JSON callback:

{
   "id":"1",
   "jsonrpc":"2.0",
   "result":{
   "articles":[
     {
        "date":1367582340000,
        "id":6917,
        "title":"Some Title",
        "author":"Name Surname",
        "event_date":1367584560000,
        "text":"blabla"            
     }
  ]
}
}

I want to deserialize it using JSON.NET

I have written the next code:

    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<Article> articles;
        [JsonProperty("articles")]
        public List<Article> 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; }
        }
    }

Usage:

 string JSON = reader.ReadToEnd();
 News ent = JsonConvert.DeserializeObject<News>(JSON) as News;

Falls with error:

An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.DLL but was not handled in user code

What is the problem?

A member with the name 'articles' already exists on 'Result'. Use the JsonPropertyAttribute to specify another name.

Make the articles field private:

private List<Article> articles;

A member with the name 'text' already exists on 'Article'. Use the JsonPropertyAttribute to specify another name.

same again:

private string text;
private int id;
private int date;
private string title;
private string author;
private string imageURL;

or better - use automatically implemented properties and throw away the fields, i.e.

[JsonProperty("text")]
public string Text {get;set;}

Arithmetic operation resulted in an overflow.

Make Date a long:

[JsonProperty("date")]
public long Date {get;set;}

Here's my entire working classes afterwards:

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<Article> articles = new List<Article>();
    [JsonProperty("articles")]
    public List<Article> 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; }
}

Note that if you are only deserializing you don't even need the JsonProperty - since the names are all identical except for the case, this would only matter if you were also serializing.