[英]Error: Newtonsoft.Json.JsonSerializationException
Hello, I am learning to use Refit and I have a problem.您好,我正在学习使用 Refit,但遇到了问题。
these are the models这些是模型
public partial class Source
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
}
public partial class Article
{
[JsonProperty("source")]
public Source Source { get; set; }
[JsonProperty("author")]
public string Author { get; set; }
[JsonProperty("title")]
public string Title { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
[JsonProperty("url")]
public Uri Url { get; set; }
[JsonProperty("urlToImage")]
public Uri UrlToImage { get; set; }
[JsonProperty("publishedAt")]
public DateTimeOffset PublishedAt { get; set; }
[JsonProperty("content")]
public string Content { get; set; }
}
This is the interface with the request and its parameters.这是带有请求及其参数的接口。
public interface IRest
{
[Get("/v2/top-headlines")]
Task<List<Article>> GetArticles(string country, string apiKey"RemovingKey");
}
This is the Request in the ArticleViewModel这是 ArticleViewModel 中的请求
private async Task GetTaskAsync()
{
var WeApp = RestService.For<IRest>("https://newsapi.org/");
var result = await WeApp.GetArticles("co");
}
When I debug to see the data this comes out当我调试以查看数据时
Newtonsoft.Json.JsonSerializationException
Message=Cannot deserialize the current JSON object (e.g. {"name":"value"})
into type 'System.Collections.Generic.List`1[RefitNpt.Models.Article]'
because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array
(e.g. [1,2,3]) or change the deserialized type so that it is a normal
.NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>)
that can be deserialized from a JSON object.
JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'status', line 1, position 10.
I've been looking for solutions but I haven't been able to solve them, I'm learning independently.我一直在寻找解决方案,但我无法解决它们,我正在独立学习。
You need another class that will serve as the RootObject for your json response.您需要另一个 class 作为 json 响应的 RootObject。
public class Articles
{
public string status {get; set;}
public int totalResults {get;set;}
public List<Article> articles {get;set; }
}
and then use this in your IRest controller, (You will need to update your method ( GetArticles
) to return Articles
instead of List<Article>
as well.然后在您的 IRest controller 中使用它,(您需要更新您的方法(
GetArticles
)以返回Articles
而不是List<Article>
。
Task<Articles> GetArticles(string country, string apiKey);
This response is aligned with following json snippet that comes from the site https://newsapi.org
此响应与来自站点
https://newsapi.org
的以下 json 片段一致
{
"status": "ok",
"totalResults": 3152,
"articles": [
{
"source": {
"id": null,
"name": "Business 2 Community"
},
"author": "Gurbaj Singh",
"title": "Investing in Cryptocurrency? What are the Odds!?",
"description": "Cryptocurrency has become the most profitable object for investing money. At the same time, in comparison with precious metals, natural…",
"url": "https://www.business2community.com/finance/investing-in-cryptocurrency-what-are-the-odds-02326876",
"urlToImage": "https://cdn.business2community.com/wp-content/uploads/2020/07/Untitled-design-3.png",
"publishedAt": "2020-07-13T15:30:31Z",
"content": "Cryptocurrency has become the most profitable object for investing money. At the same time, in comparison with precious metals, natural resources, or real estate, virtual coins involve more risks ass… [+9899 chars]"
},
// More Articles here...
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.