【发布时间】:2015-08-19 07:01:37
【问题描述】:
我已经定义了这个类来解码 json 响应
private class JsonData
{
[JsonProperty(PropertyName = "api-author")]
public string apiauthor { get; set; }
[JsonProperty(PropertyName = "api-usage")]
public string apiusage { get; set; }
[JsonProperty(PropertyName = "status")]
public string status { get; set; }
[JsonProperty(PropertyName = "language")]
public string language { get; set; }
[JsonProperty(PropertyName = "sentiment-text")]
public string sentimenttext { get; set; }
[JsonProperty(PropertyName = "sentiment-score")]
public string sentimentscore { get; set; }
}
我正在尝试调用 api
string url = "https://loudelement-free-natural-language-processing-service.p.mashape.com/nlp-text/?text=";
var response = Unirest.get(url)
.header("X-Mashape-Key", "AlZVYH30C9mshLPNM7KiE48aFfTHp1h3A31jsnmVPccxBzW5uB")
.header("Accept", "application/json")
.asJson<JsonData>()
.Body
;
var status = response.sentimenttext;
我在哪里得到这个异常
System.InvalidCastException' 类型的异常发生在 unirest-net.dll 但未在用户代码中处理
补充资料:
无法将“System.IO.MemoryStream”类型的对象转换为类型 'JsonData'。
【问题讨论】:
-
通过调用 mashape api 收到的 asjson 响应..
-
好的,让我澄清一下。
asJson中的代码是什么样的?它有什么作用?它返回什么? -
asjson 正在返回“api-author”:“Taewook Kang (taewook.kang@gmail.com)”、“api-usage”:“完全免费,只要你在你的某个地方给我信用website.", "status": "OK", "language": "english", "sentiment-text": "negative", "sentiment-score": "-0.171441"
-
可以发
asJson的代码吗? -
一旦我发现您使用的是第 3 方库,我就查看了 Unirest 的代码,我会远离这个库,因为它会造成潜在的死锁,因为它使用阻塞异步代码。我不明白为什么它会抛出那个特定的异常,但是如果您的代码都没有尝试将流大小写为 JsonData,那么可以肯定地说该库中有一些失败的假设。