异步方法我怎样才能将值绑定到IEnumerable的

问题描述 投票:0回答:1

在这里,我从服务器获取一些值

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>这里即时得到错误

c# asp.net-mvc asp.net-web-api android-asynctask
1个回答
1
投票

您正在试图看到您的回复的内容IEnumerable<>

IEnumerable<iAuth> aa  = await response.Content.ReadAsStringAsync();

ReadAsStringAsync()返回string所以这就是为什么错误出现。

所以,你需要你的Response.Content反序列化到一个特定的类型一样,

string response  = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<iAuth>(response);

现在,您可以通过使用像得到resultStatususerName

string status = result.resultStatus;
string name = result.userName;
© www.soinside.com 2019 - 2024. All rights reserved.