简体   繁体   English

JSON客户端

[英]JSON client side

The following code block runs without error but the 'data' is returned as what appears to be a single 1 dimensional record. 以下代码块运行无误,但返回的“数据”似乎是单个一维记录。 The code returns one record with all the records in it, ie if i debug and output 'data' variable to the immediate window i get : 该代码返回其中包含所有记录的一条记录,即,如果我调试并将“ 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

but element [0] to [3] is a record and [4] to [7] is a record and so on. 但是元素[0]至[3]是记录,[4]至[7]是记录,依此类推。 How do i change my code to just loop the records? 如何更改代码以仅循环记录? MAny thanks for reading, here is my code snippet below ::: 谢谢您的阅读,这是我下面的代码段:::

        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);

            });

        });

CONTROLLER CODE: 控制器代码:

    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);
    }

? Something on the lines of 一些东西

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM