Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# JSON.NET反序列化_C#_Json_Json.net_Json Deserialization - Fatal编程技术网

C# JSON.NET反序列化

C# JSON.NET反序列化,c#,json,json.net,json-deserialization,C#,Json,Json.net,Json Deserialization,我有下一个JSON回调: { "id":"1", "jsonrpc":"2.0", "result":{ "articles":[ { "date":1367582340000, "id":6917, "title":"Some Title", "author":"Name Surname", "event_date":1367584560000, "text":"b

我有下一个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<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; }
        }
    }
课堂新闻
{
私有字符串jsonrpc;
私有字符串id;
私人结果;
[JsonProperty(“jsonrpc”)]
公共字符串Jsonrpc
{
获取{return jsonrpc;}
设置{jsonrpc=value;}
}
[JsonProperty(“id”)]
公共字符串Id
{
获取{return id;}
设置{id=value;}
}
[JsonProperty(“结果”)]
公开结果
{
获取{返回结果;}
设置{result=value;}
}
}
公开课成绩
{
公开物品清单;
[JsonProperty(“条款”)]
公开物品清单
{
获取{return articles;}
设置{articles=value;}
}
}
公共类文章
{
公共字符串文本;
公共int id;
公共int日期;
公共字符串标题;
公共字符串作者;
公共字符串imageURL;
[JsonProperty(“文本”)]
公共字符串文本
{
获取{返回文本;}
设置{text=value;}
}
[JsonProperty(“id”)]
公共整数Id
{
获取{return id;}
设置{id=value;}
}
[JsonProperty(“日期”)]
公共整数日期
{
获取{返回日期;}
设置{date=value;}
}
[JsonProperty(“所有权”)]
公共字符串标题
{
获取{返回标题;}
设置{title=value;}
}
[JsonProperty(“作者”)]
公共字符串作者
{
获取{返回作者;}
设置{author=value;}
}
[JsonProperty(“imageURL”)]
公共字符串ImageURL
{
获取{return imageURL;}
设置{imageURL=value;}
}
}
用法:

 string JSON = reader.ReadToEnd();
 News ent = JsonConvert.DeserializeObject<News>(JSON) as News;
string JSON=reader.ReadToEnd();
News ent=JsonConvert.DeserializeObject(JSON)作为新闻;
出现错误:

Newtonsoft.Json.Json.JsonSerializationException类型的异常在Newtonsoft.Json.DLL中发生,但未在用户代码中处理

有什么问题

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

文章
字段
设为私有

private List<Article> articles;
或者更好-使用自动实现的属性并丢弃字段,即

[JsonProperty("text")]
public string Text {get;set;}
算术运算导致溢出

日期
设为

[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(“jsonrpc”)]
公共字符串Jsonrpc{get;set;}
[JsonProperty(“id”)]
公共字符串Id{get;set;}
[JsonProperty(“结果”)]
公共结果结果{get;set;}
}
公开课成绩
{
私有列表项目=新列表();
[JsonProperty(“条款”)]
公共列表项目{get{return Articles;}}
}
公共类文章
{
[JsonProperty(“文本”)]
公共字符串文本{get;set;}
[JsonProperty(“id”)]
公共int Id{get;set;}
[JsonProperty(“日期”)]
公共长日期{get;set;}
[JsonProperty(“所有权”)]
公共字符串标题{get;set;}
[JsonProperty(“作者”)]
公共字符串作者{get;set;}
[JsonProperty(“imageURL”)]
公共字符串ImageURL{get;set;}
}

请注意,如果您只进行反序列化,您甚至不需要
JsonProperty
——因为除了大小写之外,所有名称都是相同的,只有在您也进行序列化时,这才有意义。

@Cheese公平地说,我所做的大部分工作只是简单地阅读异常消息;这就是3个引号(“名为…”的成员”和“算术运算…”)-序列化程序会告诉您问题所在
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; }
}