3

我正在使用 mashape api:https ://market.mashape.com/montanaflynn/dictionary

这是我的代码:

HttpResponse<RootObject> response = Unirest.get("https://montanaflynn-dictionary.p.mashape.com/define?word=irony")
    .header("X-Mashape-Key", "my mashape key")
    .header("Accept", "application/json")
    .asJson<RootObject>();

我使用以下方法生成了 RootObject 类:http: //json2csharp.com/

这是我的 RootObject 类代码:

class Definition
{
    public string text { get; set; }
    public string attribution { get; set; }
}

class RootObject
{
    public List<Definition> definitions { get; set; }
}

当我运行上面的代码时,我收到以下错误:

unirest-net.dll 中出现“System.InvalidCastException”类型的未处理异常附加信息:无法将“System.IO.MemoryStream”类型的对象转换为“RootObject”类型。

问题:如何解决错误?

4

1 回答 1

0

尝试这个。它必须放在一个异步函数中:

static async Task<RootObject> GetRootInfo()
{
     HttpResponse<RootObject> response = await Unirest.get("https://montanaflynn-dictionary.p.mashape.com/define?word=irony")
    .header("X-Mashape-Key", "my mashape key")
    .header("Accept", "application/json")
    .asJsonAsync<RootObject>();

     return response.Body;
}
于 2017-01-02T16:00:01.227 回答