I have this model :
public class Person { public string Name { get; set; } public int Age { get; set; } public List<Color> Colors { get; set; } } public class Color { public int ColorId { get; set; } public string Name { get; set; } }
and asp.net MVC return Json(...) gives me the following:
[{"Name":"JC","Age":24,"Colors":[{"ColorId":1,"Name":"Red"},{"ColorId":2,"Name":"Blue"}]}, {"Name":"Albert","Age":29,"Colors":[{"ColorId":2,"Name":"Blue"}]}]
when i try to return type: List<Person>
but I want something like this (if possible):
{"People":[{"Name":"JC","Age":24,"Colors":[{"ColorId":1,"Name":"Red"},{"ColorId":2,"Name":"Blue"}]},{"Name":"Albert","Age":83,"Colors":[{"ColorId":2,"Name":"Blue"}]}]}
MY QUESTION (S):
How can I get C # (asp.net mvc) to return JSON with a better format of something like: (note: ignore the data, my main task is to return with βpeopleβ as the main collection .. how do I do this? JSON. net?)
{"People": [{"Name": "JC", "Age": 24, "Colors": [{"ColorId": 1, "Name": "Red"}, {"ColorId": 2, " Name ":" Blue "}]}, {" Name ":" Albert "," Age ": 83," Colors ": [{" ColorId ": 2," Name ":" Blue "}]}]}}
OR how can I get KNOCKOUT.JS MAPPING PLUGIN to work with this type of JSON format? (for those who know knockout)
[{"Name": "JC", "Age": 24, "Colors": [{"ColorId": 1, "Name": "Red"}, {"ColorId": 2, "Name": "Blue "}]}, {" Name ":" Albert "," Age ": 29," Colors ": [{" ColorId ": 2," Name ":" Blue "}]}]]
UPDATE (additional clarification / information):
this is my data and I want to return the list
private List<Person> _people = new List<Person> { new Person { Name = "JC", Age = 24, Colors = new List<Color> { Red, Blue, } }, new Person { Name = "Albert", Age = 29, Colors = new List<Color> { Blue } } };
in JSON format like this:
{"People":[{"Name":"JC","Age":24,"Colors":[{"ColorId":1,"Name":"Red"},{"ColorId":2,"Name":"Blue"}]}, {"Name":"Albert","Age":83,"Colors":[{"ColorId":2,"Name":"Blue"}]}]}
I'm just wondering if this is possible, or if not , how can I make the knockout.js mapping plugin adapt to the MVC json return method?