【问题标题】:Looping through JSON data.d循环遍历 JSON data.d
【发布时间】:2012-09-18 10:12:58
【问题描述】:

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
  }
]

网络服务

 <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 ?我做错了什么??

【问题讨论】:

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


    【解决方案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>');
                });
            }
        });
    });
    

    【讨论】:

    • 抱歉,它不起作用..我已经用Ajax Call 更新了我的问题...请检查一下。
    • 第一个创建返回对象列表的方法不需要返回jsons-string。
    • ant try again alert using alert(JSON.stringify(data)); now 它返回什么?
    【解决方案2】:

    您可以使用以下方法循环遍历 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 人,所以代码中可能存在一些问题,我相信您可以检查一下

    【讨论】:

    • 如何返回一个字符串数组??
    猜你喜欢
    • 1970-01-01
    • 2013-02-12
    • 1970-01-01
    • 1970-01-01
    • 2021-10-07
    • 2015-07-22
    • 2018-03-13
    • 2013-08-16
    • 2018-06-19
    相关资源
    最近更新 更多