感谢您检查我的问题。我目前正在尝试创建一个JSON,其中包含URL,链接和标题。对于使用AJAX进行的经典,典型(甚至是黑话)分页。
(首先,我使用Newton创建了一个json,但是得到了带有\ r \ n这样的代码的JSON,因为根据StackOverflow的一篇文章,默认情况下ASP.NET会处理此问题,因此使用Newton进行了两次,因此是换行代码和东西。)
因此,我试图以字典对象的形式创建相应的JSON文件,然后将其放在 return json(...,....)的最后。
我想要的json类型如下:
[
"newsItems":
[
{
"url":"/foofoo/kebab",
"title":"kebab is yummy",
"img":"/img/soKebab/png",
...
},
{
"url":"/foofoo/dimsum",
...
}
...
],
"control":
[
"hasNext":false,
"currentPage": 3,
"hasPrevious":true
]
]
为此,我写了以下内容。它继承了Umbraco.Web.Mvc.SurfaceController。
public ActionResult Pagination(int? page)
{
//instantiate five ipublishedcontents to create the json.
IEnumerable<IPublishedContent> newsPostsToRender = Umbraco.TypedContent("*id*").OrderByDescending(x =>x.UpdateDate).Skip((page.GetValueOrDefault(1) - 1) * 5).Take(5);
//instantiate an object that is going to be formed into a json and sent back to the client.
ListDictionary jsonToBeSent = new ListDictionary();
Dictionary<string, ListDictionary> newsItemsList = new Dictionary<string, ListDictionary>();
//fill and create values for "newsItems"
foreach (IPublishedContent item in newsPostsToRender)
{
//we are going to assign an array to the value of a dictionary object. So let's create the value first.
ListDictionary newsItem = new ListDictionary();
newsItem.Add("thumbnail", Umbraco.Media(item.GetPropertyValue<int>("thumb")).Url);
newsItem.Add("title", item.Name);
newsItem.Add("linkToPost", item.Url);
newsItem.Add("lastUpdate", item.UpdateDate.ToString("yyyy/MM/dd"));
//finally, I want to assign the newsItem object to newsItemsList, with its key being "newsItems"
//but I'm getting an error saying "this key already exists" whatever string I put for the key. (even gibberish such as aoihfdoasdhfa)
foreach (System.Collections.DictionaryEntry i in newsItem)
{
//Display what is assigned to newsItem to make sure the problem doesn't lie here.
System.Diagnostics.Debug.WriteLine("this is newsItem:\r\n" + i.Value);
}
System.Diagnostics.Debug.WriteLine("newsItemsList"+newsItemsList.ToString());
foreach (KeyValuePair<string, ListDictionary> ii in newsItemsList)
{
//★★★Display what is assigned to newsItemList (I think it's either null or empty), because it keeps on saying I already have whatever value I specify.★★★
System.Diagnostics.Debug.WriteLine("this is newsItemsList:\r\n" + ii.Value.Values.ToString());
if (ii.Value.Values ==null)
{
System.Diagnostics.Debug.WriteLine("this is newsItemsList is null");
}
}
//and this is the troublemaker.
newsItemsList.Add("newsItems", newsItem);
}
//next add the control element (in the form of array), and throw it back to the client.
return Json(jsonToBeSent, JsonRequestBehavior.AllowGet);
}
问题是,以下部分出现错误。
newsItemsList.Add("newsItems", newsItem);
无论我用什么字符串作为密钥,它都说“此密钥已存在”。 所以我写了一些System.Diagnostics.Debug.WriteLine();就在它之前。 并显示“ newsItemsList”如下(显示在WriteLine中,上面带有★★★)
newsItemsListSystem.Collections.Generic.Dictionary`2[System.String,System.Collections.Specialized.ListDictionary]
this is newsItemsList:
System.Collections.Specialized.ListDictionary+NodeKeyValueCollection
我的问题是:
(1)什么是“ NodeKeyValueCollection”(找不到,当我用Google搜索它时,会发现普通类的文档,它给了我实际的源代码)
(2)这是什么“ +”(表示是串联的?)
(3)我怎么看里面的东西? (有办法吗??)
(4)我为什么要得到这个??
谢谢