循环通过JSON数据

时间:2012-09-18 10:12:58

标签: c# asp.net vb.net json

data.d =

[
  {
    "NodeId": "BK01",
    "NodeName": "Books",
    "ParentId": null,
    "Likes": null
  },
  {
    "NodeId": "CO01",
    "NodeName": "Computers",
    "ParentId": null,
    "Likes": null
  },
  {
    "NodeId": "GA01",
    "NodeName": "Gaming",
    "ParentId": null,
    "Likes": null
  },
  {
    "NodeId": "MO01",
    "NodeName": "Mobile & Accessories",
    "ParentId": null,
    "Likes": null
  }
]

的WebService

 <WebMethod()> _
    Public Shared Function getCategories() As String
        Dim details As New List(Of Nodes)()
        Dim index As New Default2
        Using ds As Data.DataSet = index.db.ExecuteDataSet(CommandType.Text, "SELECT NodeID,NodeName FROM Nodes WHERE ParentID='1'")
            Dim JaggedArray As String()() = New String(ds.Tables(0).Rows.Count - 1)() {}
            Dim i As Integer = 0
            For Each rs As DataRow In ds.Tables(0).Rows
                Dim node As New Nodes()
                node.NodeId = rs("NodeId").ToString
                node.NodeName = rs("NodeName").ToString
                details.Add(node)
                'JaggedArray(i) = New String() {rs("NodeName").ToString(), rs("NodeID").ToString()}
                i = i + 1
            Next
        End Using
        Dim js As New JavaScriptSerializer()
        Dim strJSON As String = js.Serialize(details.ToArray)
        Return strJSON

    End Function

AJAX致电

<script src="js/jquery-1.7.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function() {
        //alert("!!!");
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "Default2.aspx/getCategories",
            data: "{}",
            dataType: "json",
            success: function(data) {
                var nodes = eval(data);
                alert(data.NodeName);
                $('#output').text(data.d);
                $.each(data.d, function(index, node) {
                $('#output').append('<p><strong>' + node.NodeName + ' ' +
                            node.NodeID + '</strong><br /> ');
                });
            },
            error: function(result) {
                alert("Error");
            }
        });
    });
</script>

如何循环data.d以获取值? data.d.length给了我219,但它应该是4?我做错了什么?

2 个答案:

答案 0 :(得分:1)

你可以使用$ .map代替E.G.

$(document).ready(function () {
    $.ajax({
        type: "POST",
        url: "/--service--",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: false,
        success: function (data) {
            $.map(data.d, function (item) {
                $("#name").append('<option value=' + item.ID+ '>' + item.Name + '</option>');
            });
        }
    });
});

答案 1 :(得分:1)

您可以使用:

循环访问data.d
$.each(data.d, function(index, Value)
{
      //
});

但是当你返回一个字符串,所以我认为它对你不起作用,你返回一个字符串数组会更好。 =======================编辑==================== 创建一个新类,如:

public class MyClass
{
      public MyNodes as Nodes()
}

并在getCategories的代码中执行以下操作:

Dim details As New List(Of Nodes)()
    Dim index As New Default2
Dim myObj As New MyClass
    Using ds As Data.DataSet = index.db.ExecuteDataSet(CommandType.Text, "SELECT NodeID,NodeName FROM Nodes WHERE ParentID='1'")
        Dim JaggedArray As String()() = New String(ds.Tables(0).Rows.Count - 1)() {}
        Dim i As Integer = 0
        For Each rs As DataRow In ds.Tables(0).Rows
            Dim node As New Nodes()
            node.NodeId = rs("NodeId").ToString
            node.NodeName = rs("NodeName").ToString
myObj.Add(node)
            'details.Add(node)
            'JaggedArray(i) = New String() {rs("NodeName").ToString(), rs("NodeID").ToString()}
            i = i + 1
        Next
    End Using
    Dim js As New JavaScriptSerializer()
    Dim strJSON As String = js.Serialize(myObj)
    Return strJSON

我是C#的家伙,不是VB.NET的人,所以可能是代码中有问题,我相信你可以检查