gpt4 book ai didi

c# - JSON.NET 反序列化

转载 作者:行者123 更新时间:2023-11-30 17:04:28 24 4
gpt4 key购买 nike

我有下一个 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; }
}
}

用法:

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

错误落下:

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

问题是什么?

最佳答案

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

articles 字段设置为 private:

private List<Article> articles;

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

还是一样:

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;}

Arithmetic operation resulted in an overflow.

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<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 - 因为除了大小写之外,名称都是相同的,只有当您也在序列化.

关于c# - JSON.NET 反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17446569/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com