c# - return a multidimensional json array using json.net -
i have code:
var all_books = db.books; var result = all_books.select(a => new { a.bookid, a.booktitle, a.bookisbn}).take(4).tolist(); var jsondata = new { secho = param.secho, itotalrecords = all_assets.count(), itotaldisplayrecords = all_assets.count(), aadata = result }; return json(jsondata,jsonrequestbehavior.allowget);
and json returned in form of:
{"secho":null,"itotalrecords":2764,"itotaldisplayrecords":2764,"aadata":[{"bookid":"m-711745","booktitle":"20844","bookisbn":"hp la2205wg"},{"bookid":null,"booktitle":"n/a","bookisbn":"n/a"},{"bookid":"m-710587","booktitle":"20707","bookisbn":"hp la2205wg"},{"bookid":"735129","booktitle":"21272","bookisbn":"hp la2205wg"}]}
i return multidimensional array instead.
{"secho":null,"itotalrecords":2764,"itotaldisplayrecords":2764,"aadata":[["bookid":"m-711745","booktitle":"20844","bookisbn":"hp la2205wg"],["bookid":null,"booktitle":"n/a","bookisbn":"n/a"],["bookid":"m-710587","booktitle":"20707","bookisbn":"hp la2205wg"],["bookid":"735129","booktitle":"21272","bookisbn":"hp la2205wg"]]}
i have tried code:
var result = all_books.select(a => new list<string[]>() { new string[]{ a.bookid, a.booktitle, a.bookisbn }}).take(4);
but error:
the array type 'system.string[]' cannot initialized in query result. consider using 'system.collections.generic.list`1[system.string]' instead.
what missing?
try this:
var result = all_books .select(a => new list<string> { a.bookid, a.booktitle, a.bookisbn }) .take(4) .tolist();
when serialize it, should following json, think shooting for:
{ "secho":null, "itotalrecords":2764, "itotaldisplayrecords":2764, "aadata": [ ["m-711745","20844","hp la2205wg"], [null,"n/a","n/a"], ["m-710587","hp la2205wg","20707"], ["735129","hp la2205wg","21272"] ] }
Comments
Post a Comment