Advertisement
Mirelawiel

Importer fichier JSON

Feb 19th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 1.19 KB | None | 0 0
  1. <h1>EXERCICE 3 : JSON </h1>
  2.  
  3.  
  4. <button type ="button" onclick="GenererFichier()">FichierJSON</button>
  5. <button type ="button" onclick="recup()">NuageJSON</button>
  6.  
  7.  <h5 id="ptest4"> </h5>
  8. <h5 id="resultat"> </h5>
  9.  
  10.  <script type="text/javascript">
  11.  
  12.     function GenererFichier(){
  13.         $.getJSON( "fichier.json", function(data) {
  14.         $('#ptest4').append("<ul>");
  15.         $.each( data.employees, function(i,item) {
  16.               $('#ptest4').append("<li>" + item.firstName + "  " +item.lastName+ "</li>");
  17.         });
  18.         $('#ptest4').append("</ul>");
  19. });
  20. }
  21.  
  22.  
  23.     function recup(){
  24.         $.getJSON("nuages.json", function(data){readData(data);});
  25.     }
  26.  
  27. function readData(doc){
  28.     var res = "<table><tr><th> Nom </th><th>Especes</th><th>Altitude min</th><th>Altitude max</th></tr>";
  29.     $.each(doc.nuages.nuage, function(i,item){
  30.         res +="<tr><td>"+item.nom+"</td><td>"+
  31.         liste(item.especes)+"</td><td>"+
  32.         item.altitude.min+"</td><td>"+
  33.         item.altitude.max+"</td></tr>";
  34.     });
  35.     res +="</table>";
  36.     $("#resultat").html(res);
  37. }
  38.  
  39. function liste(data){
  40.     var res = "<ul>";
  41.     $.each(data.espece, function(i,item){
  42.         res +="<li>"+item+"</li>";});
  43.     res+="</ul>";
  44.     return res;
  45.    
  46. }
  47.  
  48.  </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement