My DataTable
looks like the following:
id Descr value
CA-AB Descr1 3
CA-AC Descr2 4
CA-AD Descr3 8
With the following code, I was able to generate the following json:
string callback = Newtonsoft.Json.JsonConvert.SerializeObject(table);
byte[] resultBytes = Encoding.UTF8.GetBytes(callback);
return new System.IO.MemoryStream(resultBytes);
This is the result:
[
{"id": "CA-AB", "Descr": "Descr1", "value": "3"},
{"id": "CA-AC", "Descr": "Descr2", "value": "4"},
{"id": "CA-AD", "Descr": "Descr3", "value": "8"}
]
But now I need to change the format so it looks like this:
{
"CA-AB": {
"Descr": "Descr1",
"value": 3
},
"US-AK": {
"Descr": "Descr2",
"value": 4
},
"US-AZ": {
"Descr": "Descr3",
"value": 8
}
}
I tried something like this:
var returnData2 = new Json();
returnData2.map = "UpperTuple";
returnData2.areas = result.Tables[0];
string callback = JsonConvert.SerializeObject(returnData2);
byte[] resultBytes = Encoding.UTF8.GetBytes(callback);
return new System.IO.MemoryStream(resultBytes);
public class Json
{
// Case sensitive vvv to match your Json
public string map { get; set; }
public double zoomLevel { get; set; }
public DataTable areas { get; set; }
// you can have several constructor methods defined, I show the usage for each below.
public Json() { }
public Json(string countryMap, DataTable table, double zoom)
{
map = countryMap;
areas = table;
}
}
This creates a separate "tuple" before the actual rows, but it's obviously static, so that doesn't help much.
Any help is appreciated.