66

I am returning a List<> from a webservice as a List of JSON objects. I am trying to use a for loop to iterate through the list and grab the values out of the properties. This is a sample of the returning JSON:

{"d":[{"__type":"FluentWeb.DTO.EmployeeOrder",
 "EmployeeName":"Janet Leverling",
 "EmployeeTitle":"Sales Representative",
 "RequiredDate":"\/Date(839224800000)\/",
 "OrderedProducts":null}]}

So I am trying to extract the contents using something like this:

function PrintResults(result) {

for (var i = 0; i < result.length; i++) { 
    alert(result.employeename);
}

How should this be done?

unwind
  • 391,730
  • 64
  • 469
  • 606
Nick
  • 19,198
  • 51
  • 185
  • 312
  • IN Java, you need to map the Json into POJO. Once this done you can retrive any value. If its List then iterate(loop) it and get the json value through object – Pavan May 14 '16 at 03:09

8 Answers8

68

Be careful, d is the list.

for (var i = 0; i < result.d.length; i++) { 
    alert(result.d[i].employeename);
}
sampathsris
  • 21,564
  • 12
  • 71
  • 98
Burcu Dogan
  • 9,153
  • 4
  • 34
  • 34
57

had same problem today, Your topic helped me so here goes solution ;)

 alert(result.d[0].EmployeeTitle);
22

It's close! Try this:

for (var prop in result) {
    if (result.hasOwnProperty(prop)) {
        alert(result[prop]);
    }
}

Update:

If your result is truly is an array of one object, then you might have to do this:

for (var prop in result[0]) {
    if (result[0].hasOwnProperty(prop)) {
        alert(result[0][prop]);
    }
}

Or if you want to loop through each result in the array if there are more, try:

for (var i = 0; i < results.length; i++) {
    for (var prop in result[i]) {
        if (result[i].hasOwnProperty(prop)) {
            alert(result[i][prop]);
        }
    }
}
Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
  • 1
    This gets me closer.. It still just alerts with [object Object],[object Object].... – Nick Apr 29 '09 at 02:12
17

Here it is:

success: 
    function(data) {
        $.each(data, function(i, item){
            alert("Mine is " + i + "|" + item.title + "|" + item.key);
        });
    }

Sample JSON text:

{"title": "camp crowhouse", 
"key": "agtnZW90YWdkZXYyMXIKCxIEUG9zdBgUDA"}
bluish
  • 26,356
  • 27
  • 122
  • 180
redcrowe
  • 191
  • 1
  • 4
9

Since you are using jQuery, you might as well use the each method... Also, it seems like everything is a value of the property 'd' in this JS Object [Notation].

$.each(result.d,function(i) {
    // In case there are several values in the array 'd'
    $.each(this,function(j) {
        // Apparently doesn't work...
        alert(this.EmployeeName);
        // What about this?
        alert(result.d[i][j]['EmployeeName']);
        // Or this?
        alert(result.d[i][j].EmployeeName);
    });
});

That should work. if not, then maybe you can give us a longer example of the JSON.

Edit: If none of this stuff works then I'm starting to think there might be something wrong with the syntax of your JSON.

KyleFarris
  • 17,274
  • 5
  • 40
  • 40
  • I found the solution the hard way (not JQuery). I will try this out asap. I'd rather do it your way. I'll make sure to post the solution. – Nick Apr 29 '09 at 22:56
  • So this very, very close. Now i am looping through the array of objects. In FireBug I can see the values in the properties but the alert just returns "Undefined". How can I get the string, dates, etc out of the properties? – Nick Apr 30 '09 at 01:16
7
var d = $.parseJSON(result.d);
for(var i =0;i<d.length;i++){
    alert(d[i].EmployeeName);
}
Anne
  • 26,765
  • 9
  • 65
  • 71
Denzil Sequeira
  • 129
  • 3
  • 7
4

This will work!

$(document).ready(function ()
    {
        $.ajax(
            {
            type: 'POST',
            url: "/Home/MethodName",
            success: function (data) {
                //data is the string that the method returns in a json format, but in string
                var jsonData = JSON.parse(data); //This converts the string to json

                for (var i = 0; i < jsonData.length; i++) //The json object has lenght
                {
                    var object = jsonData[i]; //You are in the current object
                    $('#olListId').append('<li class="someclass>' + object.Atributte  + '</li>'); //now you access the property.

                }

                /* JSON EXAMPLE
                [{ "Atributte": "value" }, 
                { "Atributte": "value" }, 
                { "Atributte": "value" }]
                */
            }
        });
    });

The main thing about this is using the property exactly the same as the attribute of the JSON key-value pair.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Sterling Diaz
  • 3,789
  • 2
  • 31
  • 35
2

I have the following call:

$('#select_box_id').change(function() {
        var action = $('#my_form').attr('action');
    $.get(action,{},function(response){
        $.each(response.result,function(i) {

            alert("key is: " + i + ", val is: " + response.result[i]);

        });
    }, 'json');
    });

The structure coming back from the server look like:

{"result":{"1":"waterskiing","2":"canoeing","18":"windsurfing"}}
bluish
  • 26,356
  • 27
  • 122
  • 180
ramonhimera
  • 506
  • 5
  • 4