Hi Guys,
I am extracting some data from an SLQITE database as follows:
var newJson =[];
btnFind.onclick=function()
{
sqlList=[];
sqlList[0]=["SELECT * FROM studentData2 ", nameFound];
Sql(DB, sqlList);
}
I extract the data
function nameFound(transaction, results) {
if(results.rows.length>0) {
newJson=[];
for (var k = 0; k < results.rows.length; k++) {
var row = results.rows.item(k);
if(k > 0) newJson += ',';
newJson += '{ "name":"' + row.name + '", "age":"' + row.age + '", "Field2":"' + row.Field2 + '", "Field3":"' + row.Field3 + '", "Field4":"' + row.Field4 + '", "Field5":"' + row.Field5 + '"}';
}
jsonall = newJson + ']}';
newJson = '['+newJson+']';
alert(newJson); //shows me a correct JSON as string
// NSB.MsgBox(results.rows.item(0).name + " is " + results.rows.item(0).age);
}
I get the result.
[{ "name":"Ste", "age":"59", "Field2":"undefined", "Field3":"undefined", "Field4":"undefined", "Field5":"undefined"},{ "name":"Ste2", "age":"60", "Field2":"undefined", "Field3":"undefined", "Field4":"undefined", "Field5":"undefined"}]
I test this at https://jsonformatter.org/ and it shows as vaild Json.
If I use the following it works. ie copy the json data into
'''
var newJson = [{ "name":"Ste", "age":"59", "Field2":"undefined", "Field3":"undefined", "Field4":"undefined", "Field5":"undefined"},{ "name":"Ste2", "age":"60", "Field2":"undefined", "Field3":"undefined", "Field4":"undefined", "Field5":"undefined"}];
$("#dtContacts").dataTable().fnDestroy();
$('#dtContacts').empty();
tableContacts = $('#dtContacts').DataTable( {
'data':newJson,
'columns': [
{ 'data' : 'name' , title : 'Name'},
{ 'data': 'age' , title : 'Age'}
]
})
But if I try to use newJson directly I get errors
DataTables warning: table id=dtContacts - Requested unknown parameter 'name' for row 0, column 0. For more information about this error, please see http://datatables.net/tn/4
```
Is there something I am not understanding about the format of newJson
Cheers
Steve Warby