0

Is there a way using NewtonSoft.Json or JavaScriptSerializer to serialize a DataTable as array of arrays instead of Array of objects. For example I need to serialize the datatable as below

[["Yellow", "Large", "Cotton"],["Red", "Medium", "Linen"]]

instead of

[{"Color":"Yellow", "Size":"Large", "Make":"Cotton"},{"Color":"Red", "Size":"Medium", "Make":"Linen"}]
Alexander Petrov
  • 13,457
  • 2
  • 20
  • 49
Bhasker G
  • 217
  • 1
  • 6
  • 10
  • Related though not quite identical: [Custom JSON string output using Newtonsoft](https://stackoverflow.com/q/29957786/3744182). – dbc Oct 05 '17 at 00:27
  • Possible duplicate of [DataTable to JSON](https://stackoverflow.com/questions/451460/datatable-to-json) – Evan Carroll Dec 24 '17 at 05:53

1 Answers1

4

Try this:

JsonConvert.SerializeObject(dataTable.AsEnumerable().Select(r => r.ItemArray));
Alexander Petrov
  • 13,457
  • 2
  • 20
  • 49