Gson Requested unknown parameter
Gson Requested unknown parameter
shinkshink
Posts: 3Questions: 0Answers: 0
Hi,
I'm having problem with parsing json object to data table:
The debug report:
http://debug.datatables.net/ocitol
fiddle:
http://jsfiddle.net/eYKaK/
Error message:
DataTables warning (table id = 'unilevel_1'): Requested unknown parameter 'name' from the data source for row 0
Response Result:
[{"name":"Braw bee"}]
Entity:
[code]
public class TestKing {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
[/code]
Controller
[code]
public static Result listAssociates() {
TestKing test1 = new TestKing();
test1.setName("Braw bee");
List listTest = new ArrayList();
listTest.add(test1);
Gson gson = new Gson();
String json = gson.toJson(listTest);
return ok(json);
}
[/code]
View
[code]
var TableManaged = function () {
return {
//main function to initiate the module
init: function (data) {
alert("object: "+ data);
if (!jQuery().dataTable) {
return;
}
// begin first table
$('#unilevel_1').dataTable({
"aaData": data,
"aoColumns": [
{ "bSortable": false,
"sTitle": "FULL NAME",
"mDataProp": "name"}
]
});
jQuery('#unilevel_1_wrapper .dataTables_filter input').addClass("form-control input-medium"); // modify table search input
jQuery('#unilevel_1_wrapper .dataTables_length select').addClass("form-control input-xsmall"); // modify table per page dropdown
}
};
}();
[/code]
I'm having problem with parsing json object to data table:
The debug report:
http://debug.datatables.net/ocitol
fiddle:
http://jsfiddle.net/eYKaK/
Error message:
DataTables warning (table id = 'unilevel_1'): Requested unknown parameter 'name' from the data source for row 0
Response Result:
[{"name":"Braw bee"}]
Entity:
[code]
public class TestKing {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
[/code]
Controller
[code]
public static Result listAssociates() {
TestKing test1 = new TestKing();
test1.setName("Braw bee");
List listTest = new ArrayList();
listTest.add(test1);
Gson gson = new Gson();
String json = gson.toJson(listTest);
return ok(json);
}
[/code]
View
[code]
var TableManaged = function () {
return {
//main function to initiate the module
init: function (data) {
alert("object: "+ data);
if (!jQuery().dataTable) {
return;
}
// begin first table
$('#unilevel_1').dataTable({
"aaData": data,
"aoColumns": [
{ "bSortable": false,
"sTitle": "FULL NAME",
"mDataProp": "name"}
]
});
jQuery('#unilevel_1_wrapper .dataTables_filter input').addClass("form-control input-medium"); // modify table search input
jQuery('#unilevel_1_wrapper .dataTables_length select').addClass("form-control input-xsmall"); // modify table per page dropdown
}
};
}();
[/code]
This discussion has been closed.
Replies
when I used data2 the datatables render properly while if i used data (comes from response)
i get an error.
alert output:
[quote]
object: [{"name":"Braw bee"}] **** data2: [object Object]
[/quote]
[code]
data2 =[{"name":"Braw bee"}];
alert("object: "+ data +" **** "+"data2: "+data2);
// begin first table
$('#unilevel_1').dataTable({
"aaData":data2,
"aoColumns": [
{ "mDataProp": "name",
"bSortable": false,
"sTitle": "FULL NAME",
}
]
});
[/code]
> "aaData": "[{\"name\":\"Braw bee\"}]",
You are passing in aaData as a string - not an array. You could do:
[code]
aaData: JSON.parse( data )
[/code]
Allan
I appreciate you effort Allan! Thank you.