我有这个模型:
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; }
}
和 asp.net MVCreturn Json(...)
给了我这个:
[{"Name":"JC","Age":24,"Colors":[{"ColorId":1,"Name":"Red"},{"ColorId":2,"Name":"Blue"}]},
{"Name":"Albert","Age":29,"Colors":[{"ColorId":2,"Name":"Blue"}]}]
当我尝试返回以下类型时:List<Person>
但我想要这样的东西(如果可能的话):
{"People":[{"Name":"JC","Age":24,"Colors":[{"ColorId":1,"Name":"Red"},{"ColorId":2,"Name":"Blue"}]},{"Name":"Albert","Age":83,"Colors":[{"ColorId":2,"Name":"Blue"}]}]}
我的问题:
如何让 C# (asp.net mvc) 以更好的格式返回 JSON,例如:(注意:忽略数据,我的主要观点是它以“People”作为主要集合返回.. 我应该怎么做这个?JSON.net?)
{"人物":[{"姓名":"JC","年龄":24,"颜色":[{"ColorId":1,"Name":"Red"},{"ColorId":2,"姓名":"蓝色"}]}, {"姓名":"阿尔伯特","年龄":83,"颜色":[{"ColorId":2,"姓名":"蓝色"}]}]}
或者我怎样才能让KNOCKOUT.JS MAPPING PLUGIN使用这种类型的 JSON 格式?(对于那些知道淘汰赛的人)
[{"Name":"JC","Age":24,"Colors":[{"ColorId":1,"Name":"Red"},{"ColorId":2,"Name":"Blue "}]}, {"Name":"Albert","Age":29,"Colors":[{"ColorId":2,"Name":"Blue"}]}]
更新(额外说明/信息):
这是我的数据,我想返回一个列表
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
}
}
};
采用与此类似的 JSON 格式:
{"People":[{"Name":"JC","Age":24,"Colors":[{"ColorId":1,"Name":"Red"},{"ColorId":2,"Name":"Blue"}]},
{"Name":"Albert","Age":83,"Colors":[{"ColorId":2,"Name":"Blue"}]}]}
我只是想知道这是否可能,或者如果没有,那么我怎样才能使knockout.js 映射插件适应 MVC 的返回 json 的方式?