0

i want to iterate over a json array of arrays from th controller i want to know how i could iterate over it

i need help: nay could help me i'm new to json and mvc

//server code

        var jsonData = new
        {

            rows =
            (from bathymetrie in bathymetries

             select new
             {
                 count = bathymetries.Count,
                 Id = bathymetrie.Id,
                 date = (bathymetrie.displayedDate != null) ? 
        bathymetrie.displayedDate.ToString() : ""
             }).ToArray()
        };


  //client code

 success: function (data) {

            bathyms = "{";

            for (var i = 0; i < data[1].count; i++) {

                bathyms += el[i].Id + " : " + el[i].date;

                alert(el[i].Id);
                alert(el[i].date);
                console.log(el[i].date);

                if (i != data[0].count) {

                    bathyms += ",";
                }

            }
            bathyms += "}";
        }

2 Answers 2

1

Your data is an object with single field row, which contains an array of objects. Thus, iteration should look like this:

for (var i = 0; i < data.rows.length; i++) {
    var element = data.rows[i];
    // use element.Id, element.count and element.date
Sign up to request clarification or add additional context in comments.

Comments

1

Say if you have your model like this -

public class Data
{
    public int Id { get; set; }
    public int Count { get; set; }
    public string Date { get; set; }
}

And you are returning JsonResult of Array object like this -

    public ActionResult GetJson()
    {
        Data[] a = new Data[2];
        a[0] = new Data() { Count = 10, Id = 1, Date = "2/19/2014" };
        a[1] = new Data() { Count = 20, Id = 2, Date = "3/19/2014" };

        return new JsonResult() { Data = a };
    }

Then you can invoke this action in JQuery in the following way -

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script>
    function submitForm() {
        jQuery.ajax({
            type: "POST",
            url: "@Url.Action("GetJson")",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                $.each(data, function (key, value) {
                    alert(value.Id + ' ' + value.Count + ' ' + value.Date);
                });
            },
            failure: function (errMsg) {
                alert(errMsg);
            }
        });
    }
</script>

<input type="button" value="Click" onclick="submitForm()" />

Please observe following code which will iterate array -

        success: function (data) {
            $.each(data, function (key, value) {
                alert(value.Id + ' ' + value.Count + ' ' + value.Date);
            });

Output would be N number of alerts based on N elements in array like below -

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.