简体   繁体   English

VB.NET JSON对象问题

[英]VB.NET JSON Object Issue

UPDATE: So I've figured it out! 更新:所以我想通了! All I had to do was use jQuery.parseJSON(); 我所要做的就是使用jQuery.parseJSON(); and it converted the JSON string into a JSON Object. 然后将JSON字符串转换为JSON对象。 Here is the updated code: 这是更新的代码:

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

Ok, I've written a function to query a table and return a dataset, then convert it to JSON so the page can use it. 好的,我编写了一个函数来查询表并返回数据集,然后将其转换为JSON,以便页面可以使用它。 The function is below: 该功能如下:

    <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

The function works great in .Net 4, but I can't use it with .Net 2. So I found a JSON.Net Object called Jayrock . 该函数在.Net 4中很好用,但是我无法在.Net 2中使用它。因此,我找到了一个名为Jayrock的JSON.Net对象。 I changed my function to use it and everything works. 我更改了功能以使用它,一切正常。 Here is the revised function: 这是修改后的功能:

<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

The only problem is when it returns the JSON, the page can't parse it. 唯一的问题是当它返回JSON时,页面无法解析它。

Here is an example of the outputs from both functions: 这是两个函数的输出示例:

First Version: [object Object],[object Object],[object Object], ... 第一版: [object Object],[object Object],[object Object], ...

Revised Version: {"d":[[{"Value":"501","Display":"Record Number 501"}],[{"Value":"502","Display":"Record Number 502"}], ... 修订版本: {"d":[[{"Value":"501","Display":"Record Number 501"}],[{"Value":"502","Display":"Record Number 502"}], ...

As you can see, the revised version returns an actual string (That contains the correct data) where as the first version returned a JSON Object. 如您所见,修订版返回一个实际的字符串(其中包含正确的数据),其中第一个版本返回一个JSON对象。 I know it's because of the function returning as a String , but for the life of me, I can't figure out how to return a JSON Object using the revised code. 我知道这是因为函数以String返回,但是对于我来说,我一生都无法弄清楚如何使用修改后的代码返回JSON对象。

Does anybody have an idea on how to do this, or a better solution? 是否有人对此有想法或更好的解决方案? I'm stumped! 我很沮丧! Also, please remember that this is for .Net 2 (sucks, I know :/) 另外,请记住,这是针对.Net 2的(糟透了,我知道:/)

Thank you for your time! 感谢您的时间!

EDIT Here is some of the client side code that I'm using: 编辑这是我正在使用的一些客户端代码:

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

I had an alert(item); 我有一个alert(item); inside of the inner-most loop. 最内层循环的内部。 It was going letter by letter through the string. 整个字符串在一个字母一个字母地传递。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM