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?

1
  • 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 Commented May 14, 2016 at 3:09

8 Answers 8

68

Be careful, d is the list.

for (var i = 0; i < result.d.length; i++) { 
    alert(result.d[i].employeename);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks.. but that doesn't return anything either.
It should. That is correct. JavaScript is case sensitive. EmployeeName
57

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

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

4 Comments

Thank you. This is the conclusion I came to as well. Here is a great post that has the solution detailed. elegantcode.com/2009/05/04/jquery-ajax-with-class-arrays Hope this helps others.
the link in the comment above is dead
the link in the comment above is undead
the link in the comment above seems to redirect to linkedin
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]);
        }
    }
}

1 Comment

This gets me closer.. It still just alerts with [object Object],[object Object]....
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"}

Comments

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.

2 Comments

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.
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?
7
var d = $.parseJSON(result.d);
for(var i =0;i<d.length;i++){
    alert(d[i].EmployeeName);
}

1 Comment

here check the following link hope it helps encosia.com/never-worry-about-asp-net-ajaxs-d-again
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.

3 Comments

How about that? I think now it's better explained.
Better - without the yelling, as well. Good luck!
By the way, Sterling, your "someclass" is missing the close quote.
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"}}

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.