0

我正在尝试迭代 JSON 对象,但未能迭代它。

CS:

[WebMethod]
public static List<TicVersion> GetTicVersionList()
{
    List<TicVersion> versionList = new List<TicVersion>
                                           {
                                               new TicVersion
                                                   {
                                                       Date = DateTime.Now,
                                                       Version = "2"
                                                   },
                                               new TicVersion
                                                   {
                                                       Date = DateTime.Now,
                                                       Version = "4"
                                                   },
                                               new TicVersion
                                                   {
                                                       Date = DateTime.Now,
                                                       Version = "13"
                                                   },
                                               new TicVersion
                                                   {
                                                       Date = DateTime.Now,
                                                       Version = "28"
                                                   },
                                           };
    return versionList;
}


public class TicVersion
{
    public DateTime Date { get; set; }
    public string Version { get; set; }
}

脚本管理器

<asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" EnablePartialRendering="true"
    runat="server" />

js:

$.ajax({
    type: "POST",
    url: "ManagerTic.aspx/GetTicVersionList",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    async: false, // TODO!
    success: function (result) {
        createTicVersionList(result);
    }
});


function createTicVersionList(result) {
    var items = [];
    $.each(result.d, function (key, val) { // here is the problem.... i am not being able to get the values
        items.push('<li id="' + val.Version + '" class="ui-widget-content">' + val.Date + '</li>'); // i need the actual Date and Version 
    });
    $('<ol/>', {
        'id': 'selectable',
        'class': 'ui-selectable',
        html: items.join('')
    }).appendTo('.baseVersion');
}

编辑

> if i console.log(result) inside the each, i am getting 
> - Object
> - - d
> - - - [0]
> - - - [1]
> - - - [2]
> - - - [3]
4

1 回答 1

0

这里result.d没有意义,应该result只有。

所以在你的情况下替换这个

$.each(result.d, function (key, val) {
    // existing 
}

$.each(result, function (key, val) {
    // existing 
}

这将解决您的问题。

于 2012-10-14T08:08:47.937 回答