0

Hello I am trying to parse a .json file.. it only has 1 set of data I don't think the $.each is the best approach but it's all I can find....

Here is my code:

 <script type="text/javascript">
function getQueryVariable(variable)
{
       var query = window.location.search.substring(1);
       var vars = query.split("&");
       for (var i=0;i<vars.length;i++) {
               var pair = vars[i].split("=");
               if(pair[0] == variable){return pair[1];}
       }
       return(false);
}
var url = 'http://f.cl.ly/items/1L2k221B183J1e1G411j/200.json';

/* <![CDATA[ */
$(document).ready(function(){
    $.getJSON(url, function(data){
            $.each(data.test, function(i,test){
                content = '<h1><p class="p1"><span class="s1">' + test.name + '</span></p></h1><table class="table table-bordered"><tbody><tr><td>Section:</td><td>Chemistry</td></tr><tr><td>Synonyms:</td><td>Mg Level</td></tr><tr><td>Container:</td><td>' + test.container + '</td></tr><tr><td>Reference Ranges:</td><td>' + test.reference + '</td></tr><tr><td>Availability:</td><td>' + test.availability + '</td></tr><tr><td>Special Handling:</td><td>' + test.specialHandling + '</td></tr><tr><td>Additional Comments:</td><td>' + test.additionalComments + '/td></tr></tbody></table>';
                $(content).appendTo("#main");
                $("#main").fadeIn( );
        });
    });
}); 
/* ]]> */
</script> 

The result kinda works... the table is there but the code loops 3 times and displays "undefined" for all veribles.

Any help would be great!

1
Contributor
1
Reply
11
Views
4 Years
Discussion Span
Last Post by mattsheets
0

Got it!

<script type="text/javascript">
function getQueryVariable(variable)
{
       var query = window.location.search.substring(1);
       var vars = query.split("&");
       for (var i=0;i<vars.length;i++) {
               var pair = vars[i].split("=");
               if(pair[0] == variable){return pair[1];}
       }
       return(false);
}
var url = 'http://f.cl.ly/items/1L2k221B183J1e1G411j/200.json';
/* <![CDATA[ */
$(document).ready(function(){
    $.getJSON(url, function(data){
            content = '<h1><p class="p1"><span class="s1">' + data.test.name + '</span></p></h1><table class="table table-bordered"><tbody><tr><td>Section:</td><td>Chemistry</td></tr><tr><td>Synonyms:</td><td>Mg Level</td></tr><tr><td>Container:</td><td>' + data.test.container + '</td></tr><tr><td>Reference Ranges:</td><td>' + data.test.reference + '</td></tr><tr><td>Availability:</td><td>' + data.test.availability + '</td></tr><tr><td>Special Handling:</td><td>' + data.test.specialHandling + '</td></tr><tr><td>Additional Comments:</td><td>' + data.test.additionalComments + '/td></tr></tbody></table>';
                $(content).appendTo("#main");
                $("#main").fadeIn( );
    });
}); 
/* ]]> */
</script> 
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.