更新:所以我想通了!我所要做的只是使用jQuery.parseJSON();
,它将JSON字符串转换为JSON对象。以下是更新后的代码:VB.NET JSON对象问题
$.ajax({
type: "POST",
url: "GetEEs.aspx/GetNextEEs",
data: "{recordID:" + lastItem + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg, textStatus, jqXHR) {
var jObject = jQuery.parseJSON(msg.d);
ddl.length = 0;
$.each(jObject.d, function() {
$.each(this, function (index, item) {
addItemToDDL(ddl, item.Display, item.Value);
});
});
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
好吧,我写了一个函数来查询表并返回一个数据集,然后将其转换成JSON这样的页面可以使用它。功能如下:
<WebMethod()> _
Public Shared Function GetNextEEs(ByVal recordID As Integer) As ArrayList
If Not recordID.Equals(0) Then
Dim sql As String = "SELECT TOP 500 * FROM cbotest WHERE ID > " & recordID
Dim arr As New ArrayList
Using dr As SqlDataReader = Mangrove.SqlHelper.ExecuteReader("...")
While dr.Read()
arr.Add(New ArrayList() From { _
New With { _
Key .Value = dr("code").ToString, _
Key .Display = dr("description").ToString _
}
})
End While
End Using
Return arr
Else
Throw New ApplicationException("Invalid Record ID")
End If
End Function
功能是.NET 4的伟大工程,但我不能与.net 2.使用它,所以我发现叫Jayrock一个JSON.Net对象。我改变了我的功能来使用它,一切正常。这里是修改后的功能:
<WebMethod()> _
Public Shared Function GetNextEEs(ByVal recordID As Integer) As String
If Not recordID.Equals(0) Then
Dim sql As String = "SELECT TOP 500 * FROM cbotest WHERE ID > " & recordID
Dim json As JsonObject = New JsonObject()
Dim items As JsonArray = New JsonArray()
Using dr As SqlDataReader = Mangrove.SqlHelper.ExecuteReader("...")
While dr.Read()
Dim item As JsonArray = New JsonArray()
Dim itemArr As JsonObject = New JsonObject()
itemArr.Add("Value", dr("code").ToString)
itemArr.Add("Display", dr("description").ToString)
item.Add(itemArr)
items.Add(item)
End While
End Using
json.Add("d", items)
Using writer As JsonWriter = New EmptyJsonWriter()
json.Export(writer)
Return json.ToString
End Using
Else
Throw New ApplicationException("Invalid Record ID")
End If
End Function
唯一的问题是当它返回JSON时,页面无法解析它。
下面是这两个函数的输出的一个例子:
第一版: [object Object],[object Object],[object Object], ...
修订版本: {"d":[[{"Value":"501","Display":"Record Number 501"}],[{"Value":"502","Display":"Record Number 502"}], ...
正如你可以看到,修改后的版本,返回一个实际的字符串(这包含正确的数据)作为第一个版本返回一个JSON对象。我知道这是因为该函数返回为String
,但对于我的生活,我无法弄清楚如何使用修改后的代码返回JSON对象。
有没有人有关于如何做到这一点或更好的解决方案的想法?我很难过!另外,请记住,这是.Net 2(很烂,我知道:/)
谢谢你的时间!
编辑 下面是一些客户端代码中,我使用:
$.ajax({
type: "POST",
url: "GetEEs.aspx/GetNextEEs",
data: "{recordID:" + lastItem + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg, textStatus, jqXHR) {
//alert(msg.d);
document.getElementById('lbl').innerHTML = msg.d;
ddl.length = 0;
$.each(msg.d, function() {
$.each(this, function (index, item) {
addItemToDDL(ddl, item.Display, item.Value);
});
});
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
我有最内层的环内的alert(item);
。它通过字符串逐字逐句。
我们可以看到一些客户端代码吗? –
添加客户端代码,但不应更改,也不需要更改,更正?它之前在工作,我改变的唯一的事情就是GetNextEE功能。 –
我想'WebMethod'会自动将'd'属性添加到响应中。但Jayrock还将'd'属性添加到JSON中。所以Jayrock并不喜欢'WebMethod'。我想你应该在客户端执行'$ .each(msg.dd,function(){...}' –