key:value Json serialization
key:value Json serialization
Hi,
firstly thanks for what appears to be a great plugin.
I have come up against a few issues.
The one I have at the moment is that all Json serilizers I know of (Json.NET, JavaScriptConverter Class, + others) all generate the Json in key:value format but this is not something that the datatable seems to be able to use.
Is there a workaround for this or have I missed something or are you planning to incorporate this or...?
this is what is output from most Json serialization
[code]
{"sEcho":1,"iTotalRecords":10,"iTotalDisplayRecords":10,"aaData":[[{"Job":"Plumber","Name":"John"}],[{"Job":"Plumber2","Name":"John2"}]]}
[/code]
firstly thanks for what appears to be a great plugin.
I have come up against a few issues.
The one I have at the moment is that all Json serilizers I know of (Json.NET, JavaScriptConverter Class, + others) all generate the Json in key:value format but this is not something that the datatable seems to be able to use.
Is there a workaround for this or have I missed something or are you planning to incorporate this or...?
this is what is output from most Json serialization
[code]
{"sEcho":1,"iTotalRecords":10,"iTotalDisplayRecords":10,"aaData":[[{"Job":"Plumber","Name":"John"}],[{"Job":"Plumber2","Name":"John2"}]]}
[/code]
This discussion has been closed.
Replies
[code]
public interface ITableDataSource
{
IList GetTableData();
}
[/code]
[code]
public class Person : ITableDataSource
{
public string Job { get; set; }
public string Name { get; set; }
public string Id { get; set; }
public IList GetTableData()
{
return new List()
{
this.Id,
this.Job,
this.Name
};
}
}
[/code]
[code]
//possible client code
Person person = new Person();
Person person2 = new Person();
person.Job = "some occupation";
person.Name = "some name";
person.Id = "1";
person2.Job = "person 2 job";
person2.Name = "person 2 name";
person2.Id = "2";
List persons = new List();
persons.Add(person);
persons.Add(person2);
var o = new
{
sEcho = 1,
iTotalRecords = 10,
iTotalDisplayRecords = 10,
aaData = new List()
};
foreach (ITableDataSource ds in persons)
{
o.aaData.Add(ds.GetTableData());
}
return Json(o);
[/code]
hope this helps somebody
Nice one - thanks very much for posting your solution! I'm sure this will provide useful to others. Perhaps a future version of DataTables should allow for the key:value pair, but part of the reason I didn't do this originally was that it just adds unnecessary bandwidth requirements, since you are effectively passing redundant data around. Is there a reason you didn't want to just to an 'echo' loop, like I did in my PHP example?
Thanks,
Allan
This helps in many ways... If you have a Huge Object Graph and Composition, it introduces Dependecy.
Instead, create a Simple Json Serializer - Generic, which takes sColums(Comma Sapareted String passed by Datatable) and the List of Object you want to generate JSON , - uses Reflection(Incase of Java) to return back the Values(Json), order same as sColumns Expects...
Works like a Charm... No Need to have API's which take time to parse the whole Object and give back key:values which DataTable wont Accept
Thanks
The reason I can not build up the data exactly as I want it is I am using services to retrieve my data not from a database so I have to use their structure.
I think you are right that passing the key along everytime would be a waste of bandwidth
In that case, what I think you'll need to do is a bit of "post-processing" on the client-side to convert the data from the format you are getting from the server, into something that DataTables is expecting. In server-side processing this can be done using fnServerData. Using Ajax or whatever, it can be done just before you pass in aaData (i.e. construct aaData).
Regards,
Allan
Basically it says 'it needs to be valid json' - and it is. But it really 'it needs to be valid json without key/pair values'.
Allan