Jquery method to traverse Json data

  • 2020-06-01 08:08:37
  • OfStack

This example shows how Jquery traverses Json data. Share with you for your reference. The details are as follows:


<html>
<head>
<title>jQuery traverse json</title>
<script src="js/jquery-1.5.2.min.js" type="text/javascript"></script>
</head>
<script>
 var a = {
 "sys_bb": [
 {
  "menu_name": " Dept. ",
  "menus": [
  {
   "menu_name": " Zhang Ming "
  },
  {
   "menu_name": " Wang Ming "
  }
  ]
 },
 {
  "menu_name": " The office ",
  "menus": [
  {
   "menu_name": " Liu place "
  },
  {
   "menu_name": " wang "
  },
  {
   "menu_name": " zhang 3"
  }
  ]
 }
 ],
 "sys_cw": [
 {
  "menu_name": " The bursar's office ",
  "menus": [
  {
   "menu_name": " The tiger "
  }
  ]
 }
 ]
 };
 $(function () {
 $.each(a.sys_bb, function (i, n) {
  $("#Div").append(a.sys_bb[i].menu_name + "<br>");
  $.each(a.sys_bb[i].menus, function (i, n) {
  $("#Div").append(" " + n.menu_name + "<br>");
  });
 });
 $.each(a.sys_cw, function (i, n) {
  $("#Div").append(a.sys_cw[i].menu_name + "<br>");
  $.each(a.sys_cw[i].menus, function (i, n) {
  $("#Div").append(" " + n.menu_name + "<br>")
  });
 });
 });
</script>
<body>
<div id="Div">
</div>
</body>
</html>

I hope this article has been helpful to your jQuery programming.


Related articles: