JSON客户端

时间:2011-02-04 04:13:02

标签: javascript jquery json

以下代码块运行时没有错误,但'data'作为单个1维记录返回。代码返回一条记录,其中包含所有记录,即如果我调试并将'data'变量输出到即时窗口,我得到:

?data
{...}
    [0]: "127"
    [1]: "Barnesmore"
    [2]: "-7.934532"
    [3]: "54.692091"
    [4]: "75"
    [5]: "Beinn an Tuirc"
    [6]: "-5.583115"
    [7]: "55.565631"
    [8]: "78"
    [9]: "Beinn Tharsuinn"
    [10]: "-4.251795"
    [11]: "57.805856"
    [12]: "77"
    [13]: "Black Law"
    [14]: "-3.705482"
    [15]: "55.77749"
    [16]: "1"
    [17]: "BlackLaw Phase 3"
    [18]: "323232"
    [19]: "121212"
    20: null
    21: null
    22: null
    23: null
    length: 24

但元素[0]至[3]是记录,[4]至[7]是记录,依此类推。如何更改我的代码以循环记录?麻烦感谢阅读,这是我的代码片段:::

        var index = 0;
        var itemData;
        var url = $("#AbsolutePath").val() + "Site.mvc/GetMapList/" + "?siteDescription=" + $('#SearchTextBox').val();

        var arr = $.getJSON(url, null, function(data) {

            $.each(data, function(index, itemData) {
                debugger;
                alert(itemData);

            });

        });

控制器代码:

    public JsonResult GetMapList(string siteDescription)
    {
        var allSites = _siteRepository.FindAllSites();
        //Get all Sites that contain the value in sitedescription
        var searchResults = (from s in allSites
                             where s.SiteDescription.StartsWith(siteDescription)
                             select s);

        // We need an object client side to loop through in Javascript and add all the tags to the Map for each site in Search Results
        string[,] sArray = new string[searchResults.Count(), 4];
        int i = 0;

        foreach (Site item in searchResults)
        {
            if (item.SiteLocation == null || item.SiteLocation.SiteLocationId == 0)
            { }
            else
            {
                if ((item.SiteLocation.Latitude != 0) && (item.SiteLocation.Longitude != 0))
                {

                    sArray[i, 0] = item.SiteId.ToString();
                    sArray[i, 1] = item.SiteDescription.ToString();
                    sArray[i, 2] = item.SiteLocation.Longitude.ToString();
                    sArray[i, 3] = item.SiteLocation.Latitude.ToString();
                    i++;
                }
            }
        }
        return this.Json(sArray);
    }

1 个答案:

答案 0 :(得分:1)

的内容
for(i=0; i < data.length; i + 4 ){
  var myrecord = "Record Number: " + data[i] + ", Place: " +  data[i + 1] + ", Longitude: " + data[i + 2] + ", Latitude: " + data[i + 3];
  alert (myrecord);
}