我有一段时间与一些非常简单的json,并希望有人可以提供帮助。我有一个非常基本的VB.NET webform,它写出了使用JavaScriptSerializer进行serizlized的数据表的内容。
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))
在jquery方面我正在这样做。
$.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);
}
});
这是返回“无效的json”
这是输出
{ “状态”:[{ “状态”: “阿拉斯加”, “缩写”: “AK”, “纬度”:61.370717, “经度”: - 152.404420, “小”:空},{ “状态” : “阿拉巴马”, “缩写”: “AL”, “纬度”:32.806673, “东经”: - 86.791133, “小”:空},{ “状态”: “阿肯色州”, “缩写”: “AR”, “纬度”:34.969705, “东经”: - 92.373124, “小”:空},{ “状态”: “亚利桑那”, “缩写”: “AZ”, “纬度”:33.729761, “东经”: - 111.431224, “小”:空},{ “状态”: “加州”, “缩写”: “CA”, “纬度”:36.116203, “东经”: - 119.681563, “小”:空},{ “状态”:”科罗拉多 “ ”缩写“: ”CO“, ”纬度“:39.059810, ”经度“: - 105.311105, ”小“:空},{ ”状态“: ”康涅狄格“, ”缩写“: ”CT“,” 纬度“:38.272689,” 经度 “: - 71.367188,” 小“:真},....
提前感谢您的帮助......
答案 0 :(得分:0)
在这种情况下,你应该可以使用速记功能$.getJSON():
var url = 'StateGeoPositions.aspx';
$.getJSON(url, function(data){
alert(data);
});