如何从嵌套的json创建结构html

时间:2017-07-11 03:34:00

标签: html json each

我使用JSON创建HTML模板,但是我在创建从javascript到html的结构时遇到了问题。然后我的代码在下面



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


function showList(){
var myObj = {
"myresult" : [
  {
    "A" : [
      {
        "title"		:	"this is title A1",
        "konten"	:	"this is konten A1",
        "desc"		:	"this is description A1"
      },
      {
        "title"		:	"this is title A2",
        "konten"	:	"this is konten A2",
        "desc"		:	"this is description A2"
      },
      {
        "title"		:	"this is title A3",
        "konten"	:	"this is konten A3",
        "desc"		:	"this is description A3"
      },
    ],
    "B" : [
      {
        "title"		:	"this is title B1",
        "konten"	:	"this is konten B1",
        "desc"		:	"this is description B1"
      },
      {
        "title"		:	"this is title B2",
        "konten"	:	"this is konten B2",
        "desc"		:	"this is description B2"
      },
      {
        "title"		:	"this is title B3",
        "konten"	:	"this is konten B3",
        "desc"		:	"this is description B3"
      },
    ],
    "C" : [
      {
        "title"		:	"this is title C1",
        "konten"	:	"this is konten C1",
        "desc"		:	"this is description C1"
      },
      {
        "title"		:	"this is title C2",
        "konten"	:	"this is konten C2",
        "desc"		:	"this is description C2"
      },
      {
        "title"		:	"this is title C3",
        "konten"	:	"this is konten C3",
        "desc"		:	"this is description C3"
      },
    ]
  }
]
}

$.each(myObj, function(data){
  $.each(this, function(index, obj){
    $.each(obj, function(key, val){
     listRes(key);
    });
    $.each(obj.A, function(key, val){
      listRes(val.title);
    });
    $.each(obj.B, function(key, val){
      listRes(val.title);
    });
    $.each(obj.C, function(key, val){
      listRes(val.title);
    });
  });
});

  function listRes(title){
    var p = '<p>'+title+'</p>';
    var h2 = '<h2>'+title+'</h2>';
    var alpha = $('<div class="alphabet">' + h2 + p +'</div>');
   $('#result').append(alpha);
  }

}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id='result'>
  
</div>
&#13;
&#13;
&#13;

我期待的结果是

<div id="result">
  <div class="alphabet">
    <h2>A</h2>
    <p>this is title A1</p>
    <p>this is title A2</p>
    <p>this is title A3</p>
  </div>
  <div class="alphabet">
    <h2>B</h2>
    <p>this is title B1</p>
    <p>this is title B2</p>
    <p>this is title B3</p>
  </div>
  <div class="alphabet">
    <h2>C</h2>
    <p>this is title C1</p>
    <p>this is title C2</p>
    <p>this is title C3</p>
  </div>
</div>

如何正确循环才能得到这样的结果?

1 个答案:

答案 0 :(得分:1)

你需要一些嵌套循环来从内层访问元素,它将完成。

试试这样:

&#13;
&#13;
$(document).ready(function() {
  showList();
});


function showList() {
  var myObj = {
    "myresult": [{
      "A": [{
          "title": "this is title A1",
          "konten": "this is konten A1",
          "desc": "this is description A1"
        },
        {
          "title": "this is title A2",
          "konten": "this is konten A2",
          "desc": "this is description A2"
        },
        {
          "title": "this is title A3",
          "konten": "this is konten A3",
          "desc": "this is description A3"
        },
      ],
      "B": [{
          "title": "this is title B1",
          "konten": "this is konten B1",
          "desc": "this is description B1"
        },
        {
          "title": "this is title B2",
          "konten": "this is konten B2",
          "desc": "this is description B2"
        },
        {
          "title": "this is title B3",
          "konten": "this is konten B3",
          "desc": "this is description B3"
        },
      ],
      "C": [{
          "title": "this is title C1",
          "konten": "this is konten C1",
          "desc": "this is description C1"
        },
        {
          "title": "this is title C2",
          "konten": "this is konten C2",
          "desc": "this is description C2"
        },
        {
          "title": "this is title C3",
          "konten": "this is konten C3",
          "desc": "this is description C3"
        },
      ]
    }]
  };
  $.each(myObj.myresult, function(index, obj) {

    $.each(obj, function(idx, arr) {
      var html = '<div class="alphabet">';
      html += "<h2>" + idx + "</h2>";
      $.each(arr, function(i, v) {
        html += '<p>' + v.title + '</p>'
      });
      html += "</div>";
      $('#result').append(html);
    });


  })
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="result">
</div>
&#13;
&#13;
&#13;