6

How do you retrieve a JSON object from a local file and display it in a table using jQuery? Here is the content of JSON file (jsondata.json):

{
"scores" : [ ["3/1/2011", 610],["4/1/2011", 610],["5/1/2011", 610],["6/1/2011", 610], ["7/1/2011", 720], ["8/1/2011", 500], ["9/1/2011", 500] ]
}

3 Answers 3

16

Example - Demo http://jsfiddle.net/kVdZG/

You can iterate and append the elements.

<table id='scores' border="1"></table>  

JS -

var data = { "scores" : [ ["3/1/2011", 610],["4/1/2011", 610],["5/1/2011", 610],["6/1/2011", 610], ["7/1/2011", 720], ["8/1/2011", 500], ["9/1/2011", 500] ] }


$(data.scores).each(function(index, element){  
     $('#scores').append('<tr><td> '+element[0]+' </td> <td> '+element[1]+' </td></tr>');       
})
Sign up to request clarification or add additional context in comments.

Comments

2

jQuery does not provide any function to format JSON as a HTML table. jQuery provides only the functionality required to itterate the JSON object and manipulate the DOM. However, there are jQuery plugins that can do that.

https://github.com/gajus/json-to-table

2 Comments

Your link is dead.
This works best and you don't need jQuery: github.com/afshinm/Json-to-HTML-Table
-1
var json = { "scores" : [ ["3/1/2011", 610],["4/1/2011", 610],["5/1/2011", 610],["6/1/2011", 610], ["7/1/2011", 720], ["8/1/2011", 500], ["9/1/2011", 500] ] }

    $.each(json.scores,function(key,value){

        alert(key + " "+value)
    })

u can check from here http://jsfiddle.net/atMa7/

2 Comments

Console.log maybe better than the alert?
Or neither, since the question asks the data to be presented in a table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.