0

I have some json which is being returned from an asp.net function. I'm only testing this out so far but what I have at the moment is:

Public Structure myarray
    Dim name As String
End Structure

<WebMethod()> _
Public Shared Function temp(ByVal strTerm As String) As String

    Dim user(1) As myarray
    user(0).name = "John"
    user(1).name = "Joe"

    Dim serializer As New JavaScriptSerializer()
    Dim arrayJson As String = serializer.Serialize(user)

    Return arrayJson

End Function

and for the jquery I have:

                jQuery.ajax({
                    type: "POST",
                    url: "default.aspx/temp",
                    data: "{'strTerm':'" + req.term + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        var suggestions = [];
                        $.each(data, function (i, val) {
                            //alert(val);
                            suggestions.push(val);
                        });
                        add(suggestions);
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert('unable to create ticket');
                    }
                });

What I'm looking for in the

$.each(data, function (i, val) {
    //alert(val);
    suggestions.push(val);
 });

is to get each of the names I've specified, i.e. John & Joe and add them to the "suggestions" array .Not sure if the Json is in the correct format for this.

The json being returned is:

[{"name":"John"},{"name":"Joe"}]

Any ideas how's the correct way to do this?

Thanks,

2 Answers 2

1

Each item in your array is a json object. Therefore, if you're looking to push the names to the suggestions array, you need to push the name property of the object being iterated over.

$.each(data, function (i, val) {
    //alert(val);
    suggestions.push(val.name);
});

Note: I'd make use of a browser tool like Firebug or Chrome's built in debugger to make sure that the JSON being returned and acted on is what you're expecting. It's been my experience that ASP.NET tends to wrap its JSON response in a 'd' property, so you may need to act on data.d instead of data.

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

4 Comments

(same as above) ...yeah I've tried but all I seem to get is "undefined" if I was to alert(val.name); out. I'm wondering if its the json structure...
Have you tried data.d instead of data? Like I said, it's likely that ASP.NET has wrapped the response in a d property.
Yeah tried data.d...not quite right either. Basically I'm trying out this article in .net: net.tutsplus.com/tutorials/javascript-ajax/…
Interesting that when I try this it works: var a = [{ "name": "John" }, { "name": "Joe"}] $.each(a, function (bb) { alert(a[bb].name); });
0

If you only want to add the names do:

suggestions.push(val.name); // The result would be ['John', 'Joe']

I'm not familiar with asp, but if you work with the data in JavaScript, then JSON is the easiest way.

3 Comments

yeah I've tried but all I seem to get is "undefined" if I was to alert(val.name); out. I'm wondering if its the json structure...
inspect the structure of data with console.log(data) and user the chrome or firebug JavaScript-console.
check what JSON is actually returned. chrome dev-tool have a network section see responses and headers on a http-request basis.

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.