0

I want to import a json file when i call $(document).ready(function (){} . That properties defined in the json file are needed for my html form. How can i do that?

Ratha
  • 9,434
  • 17
  • 85
  • 163

2 Answers2

2

try like this:

  $.getJSON( "path to json file", function( data ) {
      var item = [];
      $.each( data, function( key, val ) {
        item.push( "<li id='" + key + "'>" + val + "</li>" );
      });

      $( "<ul/>", {
        "class": "myclass",
        html: item.join( "" )
      }).appendTo( "body" );
    });
Suchit kumar
  • 11,809
  • 3
  • 22
  • 44
1

An example how to do this could be:

<script type="text/javascript">
    $(function(){
        $.getJSON('names.json',function(data){
            console.log('success');
            $.each(data.employees,function(i,emp){
                $('ul').append('<li>'+emp.firstName+' '+emp.lastName+'</li>');
            });
        }).error(function(){
            console.log('error');
        });
    });
</script>
munsifali
  • 1,732
  • 2
  • 24
  • 43