Try this for the last line of your method:
string json = JsonConvert.SerializeObject(l.ToDictionary(x=>x.Name, y=>y.Value));
Result: {"foo":"bar", "biz":"baz"}
For the result: [{"foo":"bar"},{"biz":"baz"}]you can do it ...
string json = JsonConvert.SerializeObject(new object[]{new {foo="bar"}, new {biz = "baz"} });
OR
string json = JsonConvert.SerializeObject(new object[]{new Data1{foo="bar"}, new Data2{biz = "baz"} });
The first result assumes the same data type, so the results are part of the same array. The second is different data types, so you get a different array
source
share