我怎样才能改变JSON格式的asp.net MVC的方式吗? [英] How can I change asp.net mvc's way of formatting JSON?

查看:125
本文介绍了我怎样才能改变JSON格式的asp.net MVC的方式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的模式

 公共类Person
{
    公共字符串名称{;组; }
    公众诠释年龄{搞定;组; }
    公开名单<颜色和GT;颜色{搞定;组; }
}公共类颜色
{
    公众诠释ColorId {搞定;组; }
    公共字符串名称{;组; }
}

和asp.net MVC的返回JSON(...)给我这样的:

<$p$p><$c$c>[{\"Name\":\"JC\",\"Age\":24,\"Colors\":[{\"ColorId\":1,\"Name\":\"Red\"},{\"ColorId\":2,\"Name\":\"Blue\"}]},
{姓名:伟业,时代:29,颜色:[{ColorId:2,姓名:蓝}]}]

当我试图返回一个类型的:列表与LT;人&GT;

但是我想是这样的(如果可能):

<$p$p><$c$c>{\"People\":[{\"Name\":\"JC\",\"Age\":24,\"Colors\":[{\"ColorId\":1,\"Name\":\"Red\"},{\"ColorId\":2,\"Name\":\"Blue\"}]},{\"Name\":\"Albert\",\"Age\":83,\"Colors\":[{\"ColorId\":2,\"Name\":\"Blue\"}]}]}

我的问题(S):


  • 如何能我做C#(asp.net MVC)返回JSON与像一个更好的格式:(注:忽略这些数据,我的主要观点是,它以人为返回主要收集..我应该怎么做呢?JSON.net?)

    {\"People\":[{\"Name\":\"JC\",\"Age\":24,\"Colors\":[{\"ColorId\":1,\"Name\":\"Red\"},{\"ColorId\":2,\"Name\":\"Blue\"}]},
    {姓名:伟业,时代:83,颜色:[{ColorId:2,姓名:蓝}]}]}


  • 或怎样才能让我的 KNOCKOUT.JS映射插件的这种类型的JSON格式的工作? (对于那些谁知道淘汰赛)

    [{姓名:JC,时代:24,颜色:[{ColorId:1,名称:红色},{ColorId:2,名 :蓝色}]},
    {姓名:伟业,时代:29,颜色:[{ColorId:2,姓名:蓝}]}]



UPDATE(额外澄清/信息):

这是我的数据,我想返回一个List

 私人列表&LT;&人GT; _People =新的List&LT;&人GT;
                                        {
                                            新的Person
                                                {
                                                    NAME =JC
                                                    年龄= 24,
                                                    颜色=新的List&LT;颜色和GT;
                                                                {
                                                                    红,
                                                                    蓝色,
                                                                }
                                                },
                                            新的Person
                                                {
                                                    NAME =伟业,
                                                    年龄= 29,
                                                    颜色=新的List&LT;颜色和GT;
                                                                {
                                                                    蓝色
                                                                }
                                                }
                                        };

在一个类似的JSON格式:

<$p$p><$c$c>{\"People\":[{\"Name\":\"JC\",\"Age\":24,\"Colors\":[{\"ColorId\":1,\"Name\":\"Red\"},{\"ColorId\":2,\"Name\":\"Blue\"}]},
{姓名:伟业,时代:83,颜色:[{ColorId:2,姓名:蓝}]}]}

我只是想知道,如果这是可能的,或如果没有,然后我怎么才能让 knockout.js映射插件适应返回JSON的MVC的方式吗?


解决方案

您需要一个容器,因为你不希望返回数组但有一个人变的对象。

这样的事情(使用动态):

  VAR jsonData =新
            {
                人们= _People
            };
返回JSON(jsonData,JsonRequestBehavior.AllowGet);

更新

JSON是非常简单的格式。让我们跳过你不需要到现在的一切。


  • 对象:在对象JSON开始,以结束{} 。任何在他们对应的属性在C#。

  • 阵列:返回一个的IEnumerable 将返回一个数组。一个数组可以包含其他数组,对象或简单的领域。

在code以上在C#中使用动态对象,可以翻译成一个类看起来像这样:

 公共类MyCustomClass
{
    公共IEnumerable的&LT;&人GT;人们{获取;设置;}
}

因此​​,它是一个对象返回导致数组:

  {人物:[]}

{} 对应 MyCustomClass

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?

解决方案

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.

这篇关于我怎样才能改变JSON格式的asp.net MVC的方式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆