我正在尝试阅读Rest API,一切正常,我从api获取数据,但它在线上失败,它说if(response.Data.result == null)。我收到一条错误,说'Newtonsoft.Json.Linq.JArray'不包含'result'的定义。任何帮助将受到高度赞赏。这是代码:
try
{
var response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode.ToString() == "OK")
{
var responseStream = response.GetResponseStream();
StreamReader _answer = new StreamReader(responseStream);
string result = _answer.ReadToEnd();
var responseData = JsonConvert.DeserializeObject<dynamic>(result);
if (responseData.result == null || String.IsNullOrEmpty(responseData.result.ToString()))
return null;
else
{
var contactData = JsonConvert.DeserializeObject<Contact>(responseData.result.ToString());
return contactData;
}
}
}
catch (Exception ex)
{
throw ex;
}
答案 0 :(得分:0)
您可以直接检查解析的对象是否为null
:
if (responseData == null && String.IsNullOrEmpty(responseData.ToString()))
return null;