0

我目前正在尝试使用从隔离存储中返回的数据创建自定义对象列表,并将其反序列化。

它昨天工作得很好,今天一直给我这个例外,我不知道该怎么办?

{System.ArgumentOutOfRangeException: Length cannot be less than zero.
Parameter name: length
at System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy)
at System.String.Substring(Int32 startIndex, Int32 length)
at LandbouWP.ViewModel.StoryVM.GetStories(List`1 news_items)}

获取数据并反序列化的代码:

            var loaded_result = settings["mainlist"].ToString();

            var s = JsonConvert.DeserializeObject<List<Object>>(loaded_result);

反序列化工作完美,所以我认为问题不在这里,但是它可能会在列表中添加另一个属性或其他东西吗?

然后我创建一个返回项目的自定义列表

   App.StoryViewModel.GetStories(s);

该代码是:

  public void GetStories(List<Object> news_items)
    {
        List<Story> a = new List<Story>();
        List<Story> b = new List<Story>();

        //loop over all items and add them for a viewmodel
        int i = 0;
        foreach (var item in news_items)
        {
            if (item.IsDeleted == true)
            {
                //do not add the item
            }
            else
            {
                try
                {
                    a.Add(new Story
                    {
                        ID = news_items[i].ID,
                        IsDeleted = news_items[i].IsDeleted,
                        IsActive = news_items[i].IsActive,
                        Title = news_items[i].Title,
                        Author = news_items[i].Author,
                        Synopsis = news_items[i].Synopsis,
                        Body = news_items[i].Body,
                        ImageUrl = news_items[i].ImageUrl,
                        //CreationDate = DateTime.Parse(news_items[i].CreationDate),
                        CreationDate = news_items[i].CreationDate.Substring(0, news_items[i].CreationDate.IndexOf('T')),
                        LastUpdateDate = news_items[i].LastUpdateDate.Substring(0, news_items[i].LastUpdateDate.IndexOf('T')),
                        DisplayUntilDate = news_items[i].DisplayUntilDate.Substring(0, news_items[i].DisplayUntilDate.IndexOf('T')),
                        TotalViews = news_items[i].TotalViews,
                        Gallery = news_items[i].Gallery
                    });
                    i++;
                }
                catch (Exception ex)
                {
                    string msg = ex.ToString();
                    string msg2 = msg;
                }

            }
        }

        //try here to remove duplicates?
        foreach (var item in a)
        {
            if (!b.Contains(item))
            {
                b.Add(item);
            }
            else
            {
                b.Remove(item);
            }
        }

        var new_list = b.OrderByDescending(x => x.CreationDate).ToList();

        //save all the stories
        story = new_list;

我什至无法单独浏览我要设置的每个项目,它只是抛出长度不能小于零,而且我不确定它在说什么,我的类中没有名为 Length 的参数?

4

1 回答 1

1

仔细检查这个地方

CreationDate = news_items[i].CreationDate.Substring(0, news_items[i].CreationDate.IndexOf('T')),
LastUpdateDate = news_items[i].LastUpdateDate.Substring(0, news_items[i].LastUpdateDate.IndexOf('T')),
DisplayUntilDate = news_items[i].DisplayUntilDate.Substring(0, news_items[i].DisplayUntilDate.IndexOf('T')),

我想你的一个日期格式错误并且没有“T”

于 2013-08-15T11:15:07.540 回答