简体   繁体   中英

How do I debug “cannot convert to system.collections.generic.list” error

var httpClient = new HttpClient();
        var response = await httpClient.GetStringAsync("http://192.168.1.57/smsapi/api/kullanici/" + viewModel.Item.ID);
        var GrupKullanicilarList = JsonConvert.DeserializeObject<Kullanicilarr>(response);
       ---> string TelefonNoList = GetTelno(GrupKullanicilarList);


public string GetTelno(List<Kullanicilarr> GrupKullanicilarList)
    {
        List<string> TelefonNoList = new List<string>();
        foreach (Kullanicilarr Kullanici in GrupKullanicilarList)
        {
            TelefonNoList.Add("<TelefonNo><TelNo>" + Kullanici.Telefonno.ToString() + "</TelNo></TelefonNo>");
        }

        return string.Join("\n", TelefonNoList);

It gaves an error on the Fourth line in the beginning where I put arrow:"cannot convert from 'SmsApp.Models.Kullanicilarr' to 'System.Collections.Generic.List' How should I write the last line?

In line 3,

 var GrupKullanicilarList = JsonConvert.DeserializeObject<Kullanicilarr>(response);

you are deserializing the http Response as Kullanicilarr object.

and you are passing the Kullanicilarr to GetTelno(List<Kullanicilarr> GrupKullanicilarList) which expects List of Kullanicilarr. That's why you are getting this error.

Can you check if the http response is returning List<Kullanicilarr> ? If yes then you need to change line 3 as,

 var GrupKullanicilarList = JsonConvert.DeserializeObject<List<Kullanicilarr>>(response);

If not, then you need to change the method GetTelno param as GetTelno(Kullanicilarr GrupKullanicilarList) and update its functionality since you will be getting single object instead of an array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM