在C#中将DataTable转换为JSON
我想从数据库中获取记录到 DataTable
.然后将 DataTable
进入JSON对象。 将JSON对象返回给我的JavaScript函数。
string result = JsonConvert.SerializeObject(DatatableToDictionary(queryResult, "Title"), Newtonsoft.Json.Formatting.Indented);
{ "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 }}
{"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
}
]}
我该怎么做?