====Summary====
I need some help trying to format my JSON response. I'm using ASP.Net with a Model and a Controller.
====Information====
I am working on a web api in ASP.Net. I have a SQL backend in which I fetch some data and put it into a DataTable. My DataTable looks like this:
+----+-------+-------+ | ID | Title | Users | +----+-------+-------+ | 1 | Test | user1 | | 1 | Test | user2 | +----+-------+-------+
NOTE: One record can have multiple users which is why ID is "1" for both rows ("ID" is not the unique ID of the actual SQL table row but rather a foreign key...anyway, I digress... )
I have created a Model in C# that looks like this:
public class Record { public int ID { get; set; } public string Title {get; set;} public string Users {get; set;} }
Finally my Controller looks like this
DataBaseHelper db = new DataBaseHelper(); public IEnumerable Get_Record(string id) { // Get DataTable DataTable dt = new DataTable(); dt = db.GetRecord(id); foreach (DataRow row in dt.Rows) { yield return new Record { ID = row.Field("ID"), Title = row.Field("Title"), Users = row.Field("Users") }; } }
When I call the API I get this:
[ -{ ID: 1, Title: "Test", Users: "user1" }, -{ ID: 1, Title: "Test", Users: "user2" } ]
====QUESTION====
How would I get the JSON response to look something sort of like this (if possible):
{ "Response": [ { ID: 1, Title: "Test", Users: [ {name: "user1"}, {name: "user2"} ] } ] }
If that is not possible then this would be great as well:
"Response": [ { ID: 1, Title: "Test", Users: "user1" }, { ID: 1, Title: "Test", Users: "user2" } ]
Record
. As for the pretty formatting, not sure about that. – Pete Garafano