Reputation: 1764
I have the following output of json using c# json serialization. It gets populated from a C# data table. I need to add a level to the object.
UPDATE How would I make this an array of strings, instead of an array of objects?
[{
"Urgency": "C",
"Picker": null,
"CARD": null,
},
{
"Urgency": "C",
"Picker": null,
"CARD": null,
}]
the end result should be
{
"data": [
{
"Urgency": "C",
"Picker": null,
"CARD": null,
},
{
"Urgency": "C",
"Picker": null,
"CARD": null,
}
]
}
How do I add that top level to my existing json output?
Here's my loop.
foreach (DataRow dr in dt.Rows)
{
row = new Dictionary<string, object>();
foreach (DataColumn col in dt.Columns)
{
row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
context.Response.Write(serializer.Serialize(rows).ToString());
Upvotes: 0
Views: 740
Reputation: 12857
Just create an anonymous class:
foreach (DataRow dr in dt.Rows)
{
row = new Dictionary<string, object>();
foreach (DataColumn col in dt.Columns)
{
row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
context.Response.Write(serializer.Serialize(new { data= rows }).ToString());
If you are using an older version of .NET then create an actual class like this:
public class RowsDTO {
public Dictionary<string,object> Data { get; private set; }
public RowsDTO(){
data = new Dictionary<string,object>();
}
}
Upvotes: 2