0

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.

3 Answers 3

2

Try

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

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

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, this resolves the issue. One question, where did that "d" come from? Does ASP.NET automatically assign it? I did not specify it anywhere in my ScriptMethod.
It was a change added with .NET 3.5 for security reasons. See encosia.com/a-breaking-change-between-versions-of-aspnet-ajax
@user1027620: Check out this article from Encosia about the security reasons behind Microsoft's encapsulation of return objects under the d property.
0

use jquery

var obj = $.parseJSON(str)

Then you get the js object

1 Comment

His example is already an object, not a string which needs to be parsed.
0

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.

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.