Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Accessing list elements for table using json and flask I am returning a dictiona

ID: 3811293 • Letter: A

Question

Accessing list elements for table using json and flask

I am returning a dictionary(list) which is called d with keys(k) and values(v)

I want to access this in my html file and put the list into a table separated by the keys and values. In my index.html file I have the following which retrieves the list but it just simply prints it out in the html results tag.

How can I modify the html to retrieve the keys and values of the data that was returned so I am able to loop through them and put them into a table?

Explanation / Answer

$(function() { $('a#search').bind('click', function() { $.getJSON('/_search', { a: $('input[name="a"]').val() }, function(data) { // Code Modified as on 5th April 2017 section start keyValueData = JSON.parse(data.result); var tableData = ''; $.each(keyValueData, function( index, value ) { // Here index refers to the key and value refers to the value in key value pair. tableData += '' + index + '' + value; }); $('#result').append(tableData); // Code Modified as on 5th April 2017 section end }); return false; }); });