我正在使用 mashape api:https://market.mashape.com/montanaflynn/dictionary 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/ 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”类型。
问题:我该如何解决该错误?
尝试这个。它必须放在异步函数中:
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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)