0
votes

I am having a heck of a time with some very simple json and was hoping someone could help. I have a very basic VB.NET webform which which writes out the contents of a datatable serizlized with JavaScriptSerializer.

        Using conn As New SqlConnection(connStr)
        Using command As New SqlCommand("select * from stategeopositions", conn)
            conn.Open()
            Using reader As SqlDataReader = command.ExecuteReader()
                dt.TableName = "states"
                dt.Load(reader)
                conn.Close()
            End Using
        End Using
    End Using

    Dim objs As New List(Of Dictionary(Of String, Object))
    For Each row As DataRow In dt.Rows
        Dim drow As New Dictionary(Of String, Object)
        For i As Integer = 0 To dt.Columns.Count - 1
            drow.Add(dt.Columns(i).ColumnName, row(i))
        Next
        objs.Add(drow)
    Next

    Dim d As New Dictionary(Of String, Object)
    d.Add(dt.TableName, objs)

    Dim serialier As New JavaScriptSerializer

    Response.Write(serialier.Serialize(d))

On the jquery side I am doing this.

            $.ajax({
            type: "GET",
            url: 'StateGeoPositions.aspx',
            async: false,
            beforeSend: function (x) {
                if (x && x.overrideMimeType) {
                    x.overrideMimeType("application/j-son;charset=UTF-8");
                }
            },
            dataType: "json",
            success: function (data) {
                alert(data);
                //do your stuff with the JSON data
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert(xhr.status);
                alert(thrownError);
            }
        });

This is returning "invalid json"

Here is the output

{"states":[{"state":"Alaska","abbreviation":"AK","latitude":61.370717,"longitude":-152.404420,"small":null},{"state":"Alabama","abbreviation":"AL","latitude":32.806673,"longitude":-86.791133,"small":null},{"state":"Arkansas","abbreviation":"AR","latitude":34.969705,"longitude":-92.373124,"small":null},{"state":"Arizona","abbreviation":"AZ","latitude":33.729761,"longitude":-111.431224,"small":null},{"state":"California","abbreviation":"CA","latitude":36.116203,"longitude":-119.681563,"small":null},{"state":"Colorado","abbreviation":"CO","latitude":39.059810,"longitude":-105.311105,"small":null},{"state":"Connecticut","abbreviation":"CT","latitude":38.272689,"longitude":-71.367188,"small":true},....

Thank you in advance for any help...

1
Please provide all of your JSON output as what you have included currently is valid syntax.Robert
I am sorry, I am not sure what you mean? This is just the end of it. Here is 1 record. I am doing this in the page load and just using Reponse.Write(..) {"":[{"state":"Alaska","abbreviation":"AK","latitude":61.370717,"longitude":-152.404420,"small":false}]}AndySousa

1 Answers

0
votes

You should be able to use the shorthand function, $.getJSON(), in this situation:

var url = 'StateGeoPositions.aspx';

$.getJSON(url, function(data){
    alert(data);
});