如何更改 asp.net mvc 格式化 JSON 的方式?

发布于 2024-12-24 01:19:03 字数 3211 浏览 1 评论 0原文

我有这个模型

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终止放荡 2024-12-31 01:19:03

您需要一个容器,因为您不想返回数组,而是返回带有 People 变量的对象。

像这样的东西(使用动态):

var jsonData = new 
            {
                People = _people 
            };
return Json(jsonData, JsonRequestBehavior.AllowGet);

更新

JSON 是非常简单的格式。让我们跳过现在不需要的所有内容。

  • 对象:json 中的对象以 {} 开头和结尾。其中的任何内容都对应于 C# 中的属性。
  • 数组:返回 IEnumerable 将返回一个数组。数组可以包含其他数组、对象或简单字段。

上面的代码在 C# 中使用动态对象,可以转换为如下所示的类:

public class MyCustomClass
{
    public IEnumerable<Person> People {get;set;}
}

因此它是一个返回数组的对象,结果是:

{ People: [] }

其中 {} 对应于 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):

var jsonData = new 
            {
                People = _people 
            };
return Json(jsonData, JsonRequestBehavior.AllowGet);

Update

JSON is really simple format. Let's skip everything that you don't need to now.

  • Objects: Objects in json start and end with {}. Anything in them corresponds to properties in C#.
  • Arrays: Returning a 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:

public class MyCustomClass
{
    public IEnumerable<Person> People {get;set;}
}

Hence it's an object returning an array resulting in:

{ People: [] }

Where the {} corresponds to MyCustomClass.

情丝乱 2024-12-31 01:19:03

你可以返回东西,例如,这样:

 var jsonData = new
            {
                Name = qry.Name,
                Age = qry.Age,
                Colors = (
                    from c in qry
                    select new
                    {
                        ColorID = c.ColorID,
                        Name = c.Name
                    }).ToArray()
            };

return Json(jsonData, JsonRequestBehavior.AllowGet);

也许是这样的:)

you can return things, in for example, this way:

 var jsonData = new
            {
                Name = qry.Name,
                Age = qry.Age,
                Colors = (
                    from c in qry
                    select new
                    {
                        ColorID = c.ColorID,
                        Name = c.Name
                    }).ToArray()
            };

return Json(jsonData, JsonRequestBehavior.AllowGet);

Maybe something like that :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文