尝试访问该方法失败:System.Collections.Generic.List`1..ctor(

2019-10-16 18:36发布

我有这样的代码,通过它我从我的Localhost.But这是给我的title.When提到我将鼠标悬停在响应错误而debugging.It显示我正确的response.I正在使用JSON.NET到retrieveing JSON数据解析JSON响应。

var response = reader.ReadToEnd();

                   List<Company> cLst = JsonConvert.DeserializeObject<List<Company>>(response);    //Error

                    this.Dispatcher.BeginInvoke(() =>
                        {

                            foreach (Company c in cLst)
                            {
                                ListBoxItemControl Li = new ListBoxItemControl();
                                Li.CompanyNameTextBlock.Text = c.CompanyName;
                                Li.PromotionTextBlock.Text = c.PromotionText;
                                listBox1.Items.Add(Li);
                            }
                        });

这里是公司级。

class Company
{
    public string CompanyName {get; set;}
    public string CompanySlogan { get; set; }
    public string CompanyDescription { get; set; }
    public string CompanyRating { get; set; }
    public string CompanyDpPath { get; set; }
    public string CompanyOtherInfo { get; set; }
    public string CompanyFollowers { get; set; }
    public int CompanyID { get; set; }

    public int PromotionID { get; set; }
    public string PromotionText { get; set; }
    public string PromotionRating { get; set; }
    public string PromotionPicPath { get; set; }
    public string PromotionTitle { get; set; }
    public int PromotionLikes { get; set; }
    public int PromotionComments { get; set; }



}

Answer 1:

再举一个类一样,

 public class RootObject
 {
  public List<Company> companies;
 }

然后修改这样的代码,

var jsonData = JsonConvert.DeserializeObject<RootObject>(response);
List<Company> cLst = jsonData.companies;

试试这个,让我知道。



Answer 2:

尝试使公司类公共



文章来源: Attempt to access the method failed: System.Collections.Generic.List`1..ctor()