首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JSON.NET去中心化

JSON.NET去中心化
EN

Stack Overflow用户
提问于 2013-07-03 19:19:05
回答 1查看 4.8K关注 0票数 3

我有下一个JSON回调:

代码语言:javascript
复制
{
   "id":"1",
   "jsonrpc":"2.0",
   "result":{
   "articles":[
     {
        "date":1367582340000,
        "id":6917,
        "title":"Some Title",
        "author":"Name Surname",
        "event_date":1367584560000,
        "text":"blabla"            
     }
  ]
}
}

我想使用JSON.NET对其进行反序列化。

我已经写了下面的代码:

代码语言:javascript
复制
    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; }
        }
    }

用法:

代码语言:javascript
复制
 string JSON = reader.ReadToEnd();
 News ent = JsonConvert.DeserializeObject<News>(JSON) as News;

因错误而下降:

Newtonsoft.Json.DLL中出现'Newtonsoft.Json.JsonSerializationException‘类型的异常,但未在用户代码中处理

有什么问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-03 19:24:37

'Result‘上已存在名为’member‘的成员。使用JsonPropertyAttribute指定另一个名称。

articles字段设置为private

代码语言:javascript
复制
private List<Article> articles;

”中已存在名为“text”的成员。“”使用JsonPropertyAttribute指定另一个名称。

同样的:

代码语言:javascript
复制
private string text;
private int id;
private int date;
private string title;
private string author;
private string imageURL;

或者更好-使用自动实现的属性并丢弃字段,即

代码语言:javascript
复制
[JsonProperty("text")]
public string Text {get;set;}

算术运算导致溢出。

使Date成为long

代码语言:javascript
复制
[JsonProperty("date")]
public long Date {get;set;}

下面是我之后的全部工作类:

代码语言:javascript
复制
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 -因为除了大小写之外,所有名称都是相同的,只有当您也在进行序列化时,这才有意义。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17446569

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档