Convert datatable to JSON in C#

后端 未结 17 1126
猫巷女王i
猫巷女王i 2020-11-22 11:53
  1. I want to get records from database into a DataTable.
  2. Then convert the DataTable into a JSON object.
  3. Return the JSON ob
17条回答
  •  难免孤独
    2020-11-22 12:43

    Very Simple these days..

    string json = JsonConvert.SerializeObject(YourDataTable, Formatting.Indented);
    

    Now Convert your Json to a DataTable:

    YourDataTable = (DataTable)JsonConvert.DeserializeObject(json, (typeof(DataTable)));
    

    Works for DataSets as well..

提交回复
热议问题