Sam Miller
Sam Miller

Reputation: 192

Trouble with jquery loading json

Hi Can anyone help me with loading json into jquery I can only seem to get it to work in this structor

{ "name": "Zemi All", "smpx" : "2564", "scuser": "zajihi", "scfollowers": "female" }

This is the format page I have - http://newmusicproducer.com/filterable/indexj.php

My code below is:

  <script src="http://code.jquery.com/jquery-latest.js"></script>
   <script type="text/javascript" language="javascript">
   $(document).ready(function() {
      $("#driver").click(function(event){
          $.getJSON('test.json', function(data) {
            $.each(data.results,function(){
              $('#stage').html('<p> Name: ' + data.name + '</p>');
              $('#stage').append('<p> Smpx : ' + data.smpx+ '</p>');
              $('#stage').append('<p> Scuser: ' + data.scuser+ '</p>');
              $('#stage').append('<p> Scfollowers: ' + data.scfollowers+ '</p>');
            });
          });
      });
   });
   </script>
</head>
<body>
   <p>Click on the button to load results:</p>
   <div id="stage" style="background-color:blue;">
          Display area.
   </div>
   <input type="button" id="driver" value="Load Data" />
</body>

Upvotes: 0

Views: 83

Answers (1)

Dennis
Dennis

Reputation: 32598

You are iterating over data.results but the link you provided shows that the array is called testData.

$.each(data.testData, function(val){
    $('#stage').html('<p> Name: ' + val.name + '</p>')
               .append('<p> Smpx : ' + val.smpx+ '</p>')
               .append('<p> Scuser: ' + val.scuser+ '</p>')
               .append('<p> Scfollowers: ' + val.scfollowers+ '</p>');
});

Upvotes: 1

Related Questions