2016-05-27 30 views
0

我想运行一个C#控制台程序:的方法的类型参数“HttpRequest.asJson()”不能推断

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

但是,这是给我下面的错误:

Error CS0411 The type arguments for method 'HttpRequest.asJson()' cannot be inferred from the usage. Try specifying the type arguments explicitly.

有没有人有任何想法,我可能做错了什么?

回答

2

.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>>

请有一个读通过实例here

+2

请注意,如果你使用'词典<字符串,对象>',这可能是最接近无类型,你可以在这种情况下,走了。 – Jacob

相关问题