- I want to get records from a database in a
DataTable . - Then convert the
DataTable to a JSON object. - Return the JSON object in my JavaScript function.
I use this code by calling:
string result = JsonConvert.SerializeObject(DatatableToDictionary(queryResult, "Title"), Newtonsoft.Json.Formatting.Indented);
To convert a DataTable to JSON, it works correctly and returns the following:
{ "1": { "viewCount": 703, "clickCount": 98 }, "2": { "viewCount": 509, "clickCount": 85 }, "3": { "viewCount": 578, "clickCount": 86 }, "4": { "viewCount": 737, "clickCount": 108 }, "5": { "viewCount": 769, "clickCount": 130 } }
But I would like it to return the following:
{"records":[ { "Title": 1, "viewCount": 703, "clickCount": 98 }, { "Title": 2, "viewCount": 509, "clickCount": 85 }, { "Title": 3, "viewCount": 578, "clickCount": 86 }, { "Title": 4, "viewCount": 737, "clickCount": 108 }, { "Title": 5, "viewCount": 769, "clickCount": 130 } ]}
How can i do this?
json c # datatable
Rasool Ghafari Jul 01 '13 at 5:37 on 2013-07-01 05:37
source share