我正在尝试运行 C# 控制台程序:
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; }
}
}
但这给了我以下错误:
错误 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>();
此外,您没有调用 asJson
的异步版本,因此结果类型为 HttpResponse<MyClass>
,而不是 Task<HttpResponse<MyClass>>
。
请阅读此处的示例