Display partial data from a JSOn file using jquery -


i trying display id , login json file called users.json:

     {         "users": [              {                "id": 1,               "name": "ian",               "login": [                  "january 10, 2016",                  "february 1, 2016"                        ]             },              {                "id": 2,               "name": "sam",               "login": [                  "january 16, 2016",                  "february 1, 2014"                      ]                }           ]        } 

here code:

<script type="text/javascript">         $("document").ready(function(){             //get xml data , add list items             var url = "users.json";             $.getjson(url,  function (data) {                  console.log(data);                   console.log("printing data.users");                  console.log(data.users);                  $.each(data.users, function(i,val){                     $("<li></li>")                     .html(val)                     .appendto("ul#userlist");                   });              });          });      </script> </head> <body>     <h1>reading , json document using jquery.getjson()</h1>     <ul id="userlist"></ul>  </body> 

if execute above code, can see first entry json file.

if modify code this, don't see output.

  <script type="text/javascript">         $("document").ready(function(){             //get xml data , add list items             var url = "users.json";             $.getjson(url,  function (data) {                  console.log(data);                   console.log("printing data.users");                  console.log(data.users);                   var val = [];                 $.each(data.users, function(i,val){                     $("<li></li>")                     .html(val.id)                     .appendto("ul#userlist");                   });              });          });      </script> </head> <body>     <h1>reading , json document using jquery.getjson()</h1>     <ul id="userlist"></ul>   </body>  </html> 

can please me how particular set of data?

thank you!

as @charlietfl mentioned, second version have works. however, if you're looking way of doing can foreach loop. iterate on user info, , insert name. it's using foreach, ie9 , below no-go this.

var userdata = {    "users": [{          "id": 1,        "name": "ian",        "login": [          "january 10, 2016",          "february 1, 2016"        ]      },        {          "id": 2,        "name": "sam",        "login": [          "january 16, 2016",          "february 1, 2014"        ]      }    ]    };    $list = $('ul');  userdata.users.foreach(function(value, key) {        $list.append($('<li></li>').text(value.name));  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <ul></ul>


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -