在这里,我从服务器获取一些值
public class iAuth
{
public string resultStatus { get; set; }
public string userName { get; set; }
}
这IAuth我需要绑定我的数据
private async Tas<bool> GetValiedSession(string _SesToken)
{
string Baseurl = WebConfigurationManager.AppSettings["Baseurl"];
var values = new Dictionary<string, string>{
{ "productId", WebConfigurationManager.AppSettings["productId"] },
{ "productKey", WebConfigurationManager.AppSettings["productKey"] },
{ "userName", "gosoddin" },
{ "securityToken",_SesToken },
};
using (var client = new HttpClient())
{
var _json = JsonConvert.SerializeObject(values);
var content = new StringContent(_json, Encoding.UTF8, "application/json");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = await client.PostAsync(Baseurl + "validate/session", content);
//var responseString = await response.Content.ReadAsStringAsync();
IEnumerable<iAuth> aa = await response.Content.ReadAsStringAsync(); ;
// return Ok(responseString);
return true;
}
}
在这里,我怎么可以绑定值着字符串转换为system.colllection.Generic到IEnumarable<iAuth>
这里即时得到错误
您正在试图看到您的回复的内容IEnumerable<>
IEnumerable<iAuth> aa = await response.Content.ReadAsStringAsync();
但ReadAsStringAsync()
返回string
所以这就是为什么错误出现。
所以,你需要你的Response.Content
反序列化到一个特定的类型一样,
string response = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<iAuth>(response);
现在,您可以通过使用像得到resultStatus
和userName
,
string status = result.resultStatus;
string name = result.userName;