VB.NET JSON对象问题

时间:2011-06-24 16:21:41

标签: .net vb.net json jayrock

更新:所以我已经明白了!我所要做的就是使用jQuery.parseJSON();并将JSON字符串转换为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);。它通过字符串逐字逐句。

1 个答案:

答案 0 :(得分:0)

$.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);
  }
});