1

我正在尝试通过 ajax 获取 json 数据并在视图中显示它们。数据在操作方法中是正确的,但是当我想在表中显示数据时,所有的 s 都显示为未定义,而且我知道我没有这么多行。也许是因为 for 循环和每个循环但我真的不知道如何解决它。这是json数据:

      {
   "$id": "1",
   "$values": [
  {
     "$id": "2",
      "TicketId": 32,
  "Author": "5aff2283-5e71-4b10-a809-7c0235ccc18d",
  "To": "f956d006-72b7-4a4c-b5cf-9354d6e6f193",
  "Status": 1,
  "priority": 2,
  "TrackingCode": "1bbdd10553634f2fbab1769cbf0b1b44",
  "CreateDate": "2021-04-04T10:10:35.5043873",
  "IsPrivate": true,
  "IsDeleted": false,
  "User": null,
  "ApplicationRole": {
    "$id": "3",
    "Title": "\u0628\u0631\u0646\u0627\u0645\u0647 \u0646\u0648\u06CC\u0633",
    "Description": null,
    "IsDelete": false,
    "CreateDate": "2021-03-17T12:20:46.6002855",
    "EditDate": null,
    "ParentId": null,
    "Roles": null,
    "Id": "f956d006-72b7-4a4c-b5cf-9354d6e6f193",
    "Name": "programmer",
    "NormalizedName": "PROGRAMMER",
    "ConcurrencyStamp": "a76600ef-8e82-4479-915d-41ad750cad5d"
  },
  "Answer": null
},
{
  "$id": "4",
  "TicketId": 33,
  "Author": "5aff2283-5e71-4b10-a809-7c0235ccc18d",
  "To": "f956d006-72b7-4a4c-b5cf-9354d6e6f193",
  "Status": 2,
  "priority": 0,
  "TrackingCode": "f316a0d4bf9c4014a8acce1c152a56de",
  "CreateDate": "2021-04-04T10:15:09.7256989",
  "IsPrivate": false,
  "IsDeleted": false,
  "User": null,
  "ApplicationRole": {
    "$ref": "3"
  },
  "Answer": {
    "$id": "5",
    "AnswerId": 6,
    "TicketId": 33,
    "UserId": "5aff2283-5e71-4b10-a809-7c0235ccc18d",
    "AnswerText": "\u003Cp\u003Ebbbbbb\u003C/p\u003E\r\n",
    "File": null,
    "DateTime": "2021-04-05T11:03:24.6079034",
    "IsDeleted": false,
    "Ticket": {
      "$ref": "4"
    },
    "User": null
  }
} 
]
}

这是阿贾克斯:

 <script type="text/javascript">

        $(document).ready(function () {
            loadData();
        });
    function loadData() {
        $.ajax({
            url: "/Ticket/GetMyTickets",
            type: "Get",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (ListData) {                    
                var str = "";
                for (var i = 0; i < ListData.length; i++) {
                    $.each(ListData[i], function (index, items) {                          
                        str += "<tr>";
                        str += "<td>" + ListData[i].Status + "</td>";
                        str += "<td>" + ListData[i].priority + "</td>";
                        str += "<td>" + ListData[i].TrackingCode + "</td>";
                        str += "<td>" + ListData[i].CreateDate + "</td>";
                        str += "<td>" + ListData[i].IsPrivate + "</td>";
                        str += "<td>" + ListData[i].To + "</td>";
                        str += "<td><a onclick=Edit(this) data-toggle='modal' data- 
          target='#myModal'>نمایش پاسخ ها</a></td>";

                        str += "</tr>";
                    },
                BodyData.innerHTML = str);
                    $(".TblData tr").fadeIn(1000);

                };

                    }
        });
    }

我进行了很多搜索,但没有找到任何解决该问题的方法。

更新这是 console.log(ListData) 的输出:

  {
 "$id": "1",
 "$values": [
      {
      "$id": "2",
     "TicketId": 33,
     "Author": "5aff2283-5e71-4b10-a809-7c0235ccc18d",
     "To": "f956d006-72b7-4a4c-b5cf-9354d6e6f193",
     "Status": 2,
       "priority": 0,
      "TrackingCode": "f316a0d4bf9c4014a8acce1c152a56de",
      "CreateDate": "2021-04-04T10:15:09.7256989",
       "IsPrivate": false,
         "IsDeleted": false,
       "User": {
       "$id": "3",
        "FullName": "\u0645\u062F\u06CC\u0631 \u0633\u0627\u06CC\u062A",
      "Avatar": "DefualtAvatar.jpg",
        "IsDelete": false,
      "CreateDate": "2021-03-16T13:05:20.6549686",
        "EditDate": null,
         "Id": "5aff2283-5e71-4b10-a809-7c0235ccc18d",
       "UserName": "09876543210",
         "NormalizedUserName": "09876543210",
        "Email": "nikitmb2@gmail.com",
          "NormalizedEmail": null,
          "EmailConfirmed": false,
          "PasswordHash": 
      "AQAAAAEAACcQAAAAEFbvXk/J1tXDz/
        gAkIwWVjGWwSCeVhRgpPM0PVaRt1NJ1xlNr8HhxtMNWgd2u40BQw==",
     "SecurityStamp": "IELKDSXBF5RE3MFHN7CNVOIGWWMK4QAV",
       "ConcurrencyStamp": "517cf6f0-2f41-44a6-9a99-9512e0ffc471",
    "PhoneNumber": "09876543210",
    "PhoneNumberConfirmed": false,
    "TwoFactorEnabled": false,
    "LockoutEnd": null,
    "LockoutEnabled": true,
    "AccessFailedCount": 0
  },
  "ApplicationRole": {
    "$id": "4",
    "Title": "\u0628\u0631\u0646\u0627\u0645\u0647 \u0646\u0648\u06CC\u0633",
    "Description": null,
    "IsDelete": false,
    "CreateDate": "2021-03-17T12:20:46.6002855",
    "EditDate": null,
    "ParentId": null,
    "Roles": null,
    "Id": "f956d006-72b7-4a4c-b5cf-9354d6e6f193",
    "Name": "programmer",
    "NormalizedName": "PROGRAMMER",
    "ConcurrencyStamp": "a76600ef-8e82-4479-915d-41ad750cad5d"
  },
  "Answer": {
    "$id": "5",
    "AnswerId": 6,
    "TicketId": 33,
    "UserId": "5aff2283-5e71-4b10-a809-7c0235ccc18d",
    "AnswerText": "\u003Cp\u003Ebbbbbb\u003C/p\u003E\r\n",
    "File": null,
    "DateTime": "2021-04-05T11:03:24.6079034",
    "IsDeleted": false,
    "Ticket": {
      "$ref": "2"
    },
    "User": {
      "$ref": "3"
    }
  }
 }
  ]
  }
4

1 回答 1

0

根据您的 json 结构,您只需要一个循环即可遍历您的 json 数组。因此,只需使用ListData["$values"]inside $.each,然后使用 keyname 获取相应的值。

演示代码

//just for demo
var ListData = {
  "$id": "1",
  "$values": [{
      "$id": "2",
      "TicketId": 32,
      "Author": "5aff2283-5e71-4b10-a809-7c0235ccc18d",
      "To": "f956d006-72b7-4a4c-b5cf-9354d6e6f193",
      "Status": 1,
      "priority": 2,
      "TrackingCode": "1bbdd10553634f2fbab1769cbf0b1b44",
      "CreateDate": "2021-04-04T10:10:35.5043873",
      "IsPrivate": true,
      "IsDeleted": false,
      "User": null,
      "ApplicationRole": {
        "$id": "3",
        "Title": "\u0628\u0631\u0646\u0627\u0645\u0647 \u0646\u0648\u06CC\u0633",
        "Description": null,
        "IsDelete": false,
        "CreateDate": "2021-03-17T12:20:46.6002855",
        "EditDate": null,
        "ParentId": null,
        "Roles": null,
        "Id": "f956d006-72b7-4a4c-b5cf-9354d6e6f193",
        "Name": "programmer",
        "NormalizedName": "PROGRAMMER",
        "ConcurrencyStamp": "a76600ef-8e82-4479-915d-41ad750cad5d"
      },
      "Answer": null
    },
    {
      "$id": "4",
      "TicketId": 33,
      "Author": "5aff2283-5e71-4b10-a809-7c0235ccc18d",
      "To": "f956d006-72b7-4a4c-b5cf-9354d6e6f193",
      "Status": 2,
      "priority": 0,
      "TrackingCode": "f316a0d4bf9c4014a8acce1c152a56de",
      "CreateDate": "2021-04-04T10:15:09.7256989",
      "IsPrivate": false,
      "IsDeleted": false,
      "User": null,
      "ApplicationRole": {
        "$ref": "3"
      },
      "Answer": {
        "$id": "5",
        "AnswerId": 6,
        "TicketId": 33,
        "UserId": "5aff2283-5e71-4b10-a809-7c0235ccc18d",
        "AnswerText": "\u003Cp\u003Ebbbbbb\u003C/p\u003E\r\n",
        "File": null,
        "DateTime": "2021-04-05T11:03:24.6079034",
        "IsDeleted": false,
        "Ticket": {
          "$ref": "4"
        },
        "User": null
      }
    }
  ]
}

$(document).ready(function() {
  loadData();
});

function loadData() {
  /* $.ajax({
     url: "/Ticket/GetMyTickets",
     type: "Get",
     contentType: "application/json; charset=utf-8",
     dataType: "json",
     success: function(ListData) {*/
  var str = "";
  //get values array
  $.each(ListData["$values"], function(index, items) {
    //use items.nameofkey
    str += "<tr>";
    str += "<td>" + items.Status + "</td>";
    str += "<td>" + items.priority + "</td>";
    str += "<td>" + items.TrackingCode + "</td>";
    str += "<td>" + items.CreateDate + "</td>";
    str += "<td>" + items.IsPrivate + "</td>";
    str += "<td>" + items.To + "</td>";
    str += "<td><a href='#' onclick='Edit(this)' data-toggle='modal' data-target='#myModal'> نمایش پاسخ ها </a></td> ";
    str += "</tr>";
  });
  $(".TblData").html(str)
  $(".TblData").fadeIn(1000);
  /* };

    }
  });*/
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table border="1">
  <tbody class="TblData"></tbody>
</table>

于 2021-04-11T13:00:39.830 回答