Mapping a json in jas

3

Good afternoon, can someone help me map this json in a javascript ?

[{
    "header": {
        "nav" : {"option1":"menu 1"}
    }
}]

so that this stays this way

<header>
    <nav>
        <li></li>
    </nav>
</header>
    
asked by David Restrepo 12.01.2017 в 18:20
source

1 answer

1

The truth is that, as the comrades comment, it is not very clear what you ask or how you get from the json to the html but, doing an exercise of imagination and assuming that you are not using any framework, to see if this is what you need:

  • Having the elements in an array you could go through them and mount the html by hand in a variable.
  • Once you have everything. You use getElementById to put the value in the element you want (I understand that in the nav tag). I leave a link for you to see what it does: w3schools

Here is a small example to see if this is what you want:

var menus = [
  {"option":"menu 1"},
  {"option":"menu 2"},
  {"option":"menu 3"},
  {"option":"menu 4"}
];
var html="";
for (var i = 0; i < menus.length; i++) {
  html=html+"<li>" + menus[i].option + "</li>";
}
document.getElementById("menu").innerHTML=html;
<html>
  <head>
  </head>
  <body>
    <header>
      <nav>
        <ul id="menu"></ul>
      </nav>
    </header>
  </body>
</html>
    
answered by 13.01.2017 в 08:27