【问题标题】:Attempt to access the method failed: System.Collections.Generic.List`1..ctor()尝试访问该方法失败:System.Collections.Generic.List`1..ctor()
【发布时间】:2012-05-28 10:46:10
【问题描述】:

我有这段代码,我通过它从我的本地主机中检索 json 数据。但它给出了我的标题中提到的错误。当我在调试时将鼠标悬停在响应上时。它显示了正确的响应。我正在使用 JSON .NET 解析 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; }



}

【问题讨论】:

  • JSON 是什么样的?

标签: json windows-phone-7 json.net


【解决方案1】:

尝试公开 Company 类

【讨论】:

    【解决方案2】:

    参加另一门课,例如,

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

    然后像这样修改你的代码,

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

    试试这个并告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-13
      相关资源
      最近更新 更多