如何更改 asp.net mvc 格式化 JSON 的方式?
我有这个模型:
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 MVC的return Json(...)
给了我这个:
[{"Name":"JC","Age":24,"Colors":[{"ColorId":1,"Name":"Red"},{"ColorId":2,"Name":"Blue"}]},
{"Name":"Albert","Age":29,"Colors":[{"ColorId":2,"Name":"Blue"}]}]
当我尝试返回以下类型时:List
但我想要这样的东西(如果可能的话):
{"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,"姓名":"红色"},{"ColorId": 2,"名称":"蓝色"}]}, {"Name":"Albert","Age":83,"Colors":[{"ColorId":2,"Name":"Blue"}]}]}
或者我该如何制作KNOCKOUT.JS MAPPING PLUGIN 可以使用这种类型的 JSON 格式吗? (对于那些知道淘汰赛的人)
[{"姓名":"JC","年龄":24,"颜色":[{"ColorId":1,"姓名":"红色"},{"ColorId":2,"姓名" :“蓝色的”}]}, {"Name":"Albert","Age":29,"Colors":[{"ColorId":2,"Name":"Blue"}]}]
更新(额外说明/信息):< /strong>
这是我的数据,我想返回一个
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"}]}]}
我只是想知道这是否可能,或者如果不可能,那么我该如何制作淘汰赛.js映射插件适配MVC返回json的方式?
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's return Json(...)
gives me this:
[{"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 a type of: 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 make C# (asp.net mvc) return JSON with a better format of something like: (note: ignore the data, my main point is for it to return with "People" as the main collection.. how should 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 make KNOCKOUT.JS MAPPING PLUGIN 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 (extra clarification/info):
this is my data, and I want to return a 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 a JSON format similar to 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 that is possible, or if not, then how can I make the knockout.js mapping plugin adapt to MVC's way of returning json?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要一个容器,因为您不想返回数组,而是返回带有 People 变量的对象。
像这样的东西(使用动态):
更新
JSON 是非常简单的格式。让我们跳过现在不需要的所有内容。
{}
开头和结尾。其中的任何内容都对应于 C# 中的属性。IEnumerable
将返回一个数组。数组可以包含其他数组、对象或简单字段。上面的代码在 C# 中使用动态对象,可以转换为如下所示的类:
因此它是一个返回数组的对象,结果是:
其中
{}
对应于MyCustomClass.
You need a container since you do not want to return an array but an object with a People variable.
Something like this (using dynamic):
Update
JSON is really simple format. Let's skip everything that you don't need to now.
{}
. Anything in them corresponds to properties in C#.IEnumerable
will return an array. An array can contain other arrays, objects or simple fields.The code above is using a dynamic object in C# and can be translated into a class looking like this:
Hence it's an object returning an array resulting in:
Where the
{}
corresponds toMyCustomClass
.你可以返回东西,例如,这样:
也许是这样的:)
you can return things, in for example, this way:
Maybe something like that :)