user1027620
user1027620

Reputation: 2785

Getting C# Object Property Values with jQuery JSON

So I have this:

{"d":[{"__type":"Like","Id":345,"Sender":"JohnSmith","SourceId":338,"DateTime":"\/Date(1321057654000)\/","FromStream":true}]}

And this:

        function LikesSuccess(result, userContext, methodName) {
            for (var key in result) {
                alert(key.Sender);
            }
        }

JSON returns an array of type "Like" with the properties shown above. Is there another way of getting "JohnSmith" from Sender? Because this returns undefined.

Thanks.

Upvotes: 0

Views: 1256

Answers (3)

Jim H.
Jim H.

Reputation: 5579

Try

for (var i = 0; i < result.d.length; i++) {
    alert(result.d[i].Sender);
}

because your JSON object has the key d. >> jsfiddle

Upvotes: 2

Pawan Mishra
Pawan Mishra

Reputation: 7268

Try this code :

function LikesSuccess(result, userContext, methodName) {
            for (var property in result.Properties) {
                alert(result[property]);
            }
        }

I remember having used something similar, when I was working with Asp.Net Ajax, web services. Not sure about the correct syntax though.

Upvotes: 0

ComfortablyNumb
ComfortablyNumb

Reputation: 3501

use jquery

var obj = $.parseJSON(str)

Then you get the js object

Upvotes: 0

Related Questions