C# 方法';HttpRequest.asJson()';无法推断
我正在尝试运行一个C#控制台程序:C# 方法';HttpRequest.asJson()';无法推断,c#,unirest,C#,Unirest,我正在尝试运行一个C#控制台程序: namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Task<HttpResponse<MyClass>> response = Unirest.get("https://wordsapiv1.p.mashape.com/words/cat/rhymes
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Task<HttpResponse<MyClass>> response = Unirest.get("https://wordsapiv1.p.mashape.com/words/cat/rhymes")
.header("X-Mashape-Key", "xxx")
.header("Accept", "application/json")
.asJson();
}
}
internal class MyClass
{
public string word { get; set; }
}
}
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
任务响应=Unirest.get(“https://wordsapiv1.p.mashape.com/words/cat/rhymes")
.标题(“X-Mashape-Key”、“xxx”)
.header(“接受”、“应用程序/json”)
.asJson();
}
}
内部类MyClass
{
公共字符串字{get;set;}
}
}
但这给了我以下错误:
错误CS0411方法“HttpRequest.asJson()”的类型参数
无法从用法推断。尝试指定类型参数
明确地说
有人知道我做错了什么吗?.asJson()
需要知道json应该反序列化成什么类型。在本例中,您使用的是MyClass
。
将代码更改为以下内容:
HttpResponse<MyClass> response = Unirest.get("https://wordsapiv1.p.mashape.com/words/cat/rhymes")
.header("X-Mashape-Key", "xxx")
.header("Accept", "application/json")
.asJson<MyClass>();
HttpResponse response=Unirest.get(“https://wordsapiv1.p.mashape.com/words/cat/rhymes")
.标题(“X-Mashape-Key”、“xxx”)
.header(“接受”、“应用程序/json”)
.asJson();
另外,您没有调用异步版本的asJson
,因此结果类型是HttpResponse
,而不是Task
请仔细阅读示例.asJson()
需要知道json应该反序列化成什么类型。在本例中,您使用的是MyClass
。
将代码更改为以下内容:
HttpResponse<MyClass> response = Unirest.get("https://wordsapiv1.p.mashape.com/words/cat/rhymes")
.header("X-Mashape-Key", "xxx")
.header("Accept", "application/json")
.asJson<MyClass>();
HttpResponse response=Unirest.get(“https://wordsapiv1.p.mashape.com/words/cat/rhymes")
.标题(“X-Mashape-Key”、“xxx”)
.header(“接受”、“应用程序/json”)
.asJson();
另外,您没有调用异步版本的asJson
,因此结果类型是HttpResponse
,而不是Task
请仔细阅读示例注意,如果您使用
字典
,则可能是本例中最接近无类型的。请注意,如果您使用字典
,则可能是本例中最接近无类型的。