更改返回 JsonResult 的 MVC2 控制器中的 JSON 密钥文本

发布于 2024-10-08 22:01:40 字数 2239 浏览 6 评论 0原文

我正在使用 JsonResult 作为大型 MVC2 应用程序的一部分,Web 服务将由 iPhone 应用程序使用。应用程序开发人员建议缩写键名,以减少通过移动网络发送的数据量。我目前有几个使用 MVC JsonResult 返回的类,例如 PrimaryCategories 类:

public class PrimaryCategory
{
    public long Id { get; set; }
    public string Name { get; set; }
    public string Image { get; set; }
    public List<long> SubCategories { get; set; }
    public bool IsVisible { get; set; }
}

当通过 JsonResult 填充和返回时,返回以下内容:

[
    {
        "Id":1,
        "Name":"Animals",
        "Image":"/Content/Categories/1.png",
        "SubCategories":[8,9,10],
        "IsVisible":true
    },
    {
        "Id":2,
        "Name":"Birds",
        "Image":"/Content/Categories/2.png",
        "SubCategories":[11,12,13],
        "IsVisible":true
    }
]

是否有一些内置方法可以用属性标记属性,或为 JavaScriptSerializer 提供元数据提供缩写属性名称和长属性名称之间的映射,以便返回以下内容:

[
    {
        "id":1,
        "n":"Animals",
        "img":"/Content/Categories/1.png",
        "sc":[8,9,10],
        "vis":true
    },
    {
        "id":2,
        "n":"Birds",
        "img":"/Content/Categories/2.png",
        "sc":[11,12,13],
        "vis":true
    }
]

目前我最好的解决方案是用 ScriptIgnore 然后提供第二组属性,它们使用长命名属性作为后备存储:

public class PrimaryCategory
{
    [ScriptIgnore]
    public long Id { get; set; }

    [ScriptIgnore]
    public string Name { get; set; }

    // etc.

    public long id
    {
        get { return Id; }
        set { Id = value; }
    }

    public string n
    {
        get { return Name; }
        set { Name = value; }
    }

    // etc.
}

这似乎过于冗长。我的第一个想法是,也许有一个类似于 [ScriptIgnore] 的属性可以让我为 JSON 键指定一个名称,但是我似乎无法在 MSDN 上或通过 Reflector 找到一个属性。我的第二个偏好是注入(我对我自己的类使用 NInject)一个供 MVC2 使用的备用 JavaScript 序列化器,以支持此类属性,但是:

  1. 我不知道从哪里开始注入供 MVC 使用的备用 JavaScriptSerializer
  2. 可以吗?以某种方式扩展现有的 JavaScriptSerializer 类,它似乎不开放扩展,也不实现接口或从任何东西继承(除了对象)。

任何帮助、意见和建议表示赞赏。

I am creating a small number of JSON "Web Services" using JsonResult as part of a large MVC2 application, the Web Services will be consumed by an iPhone app. The app developer has suggested that the key names be abbreviated to reduce the quantity of data being sent across a mobile network. I currently have several classes which I return with the MVC JsonResult such as the PrimaryCategories Class:

public class PrimaryCategory
{
    public long Id { get; set; }
    public string Name { get; set; }
    public string Image { get; set; }
    public List<long> SubCategories { get; set; }
    public bool IsVisible { get; set; }
}

When populated and returned through the JsonResult the following is returned:

[
    {
        "Id":1,
        "Name":"Animals",
        "Image":"/Content/Categories/1.png",
        "SubCategories":[8,9,10],
        "IsVisible":true
    },
    {
        "Id":2,
        "Name":"Birds",
        "Image":"/Content/Categories/2.png",
        "SubCategories":[11,12,13],
        "IsVisible":true
    }
]

Is there some built in way of tagging the properties with attributes, or providing the JavaScriptSerializer with meta data to provide mapping between abbreviated property names and long property names, such that the following is returned:

[
    {
        "id":1,
        "n":"Animals",
        "img":"/Content/Categories/1.png",
        "sc":[8,9,10],
        "vis":true
    },
    {
        "id":2,
        "n":"Birds",
        "img":"/Content/Categories/2.png",
        "sc":[11,12,13],
        "vis":true
    }
]

At present my best solution is to mark each of the properties with ScriptIgnore then provide a second set of properties that use the long-named properties as a backing store:

public class PrimaryCategory
{
    [ScriptIgnore]
    public long Id { get; set; }

    [ScriptIgnore]
    public string Name { get; set; }

    // etc.

    public long id
    {
        get { return Id; }
        set { Id = value; }
    }

    public string n
    {
        get { return Name; }
        set { Name = value; }
    }

    // etc.
}

This seems overly verbose. My first thought was that perhaps there was an attribute that similar to [ScriptIgnore] that would allow me to specify a name for the JSON key, however I can not seem to find one on MSDN or through Reflector. My second preference would be to inject (I use NInject for my own classes) an alternate JavaScript serializer for MVC2 to use that supported such an attribute, however:

  1. I don't know where to start injecting an alternate JavaScriptSerializer for MVC to use
  2. Can I extend the existing JavaScriptSerializer class in some way, it dosn't seem to be open for extension, nor does it implement an Interface or inherit from anything (other than Object).

Any help, comments and advice appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

落花浅忆 2024-10-15 22:01:40

为什么不简单地选择具有适当名称的匿名对象呢?或者,如果您在许多地方执行此操作,请使用一个具有短名称的特定于视图的类作为结果。然后序列化匿名对象(或视图类)而不是您的业务实体。

var categories = primaryCategories.Select( pc => new
                 {
                     id : pc.Id,
                     n  : pc.Name,
                     img: pc.Image,
                     sc : pc.SubCategories,
                     vis: pc.IsVisible
                 });

return Json( categories );

Why not simply select into an anonymous object with the appropriate names? Or if you're doing this in many places, have a view-specific class with short names that is used as the result. Then serialize the anonymous object (or view class) instead of your business entity.

var categories = primaryCategories.Select( pc => new
                 {
                     id : pc.Id,
                     n  : pc.Name,
                     img: pc.Image,
                     sc : pc.SubCategories,
                     vis: pc.IsVisible
                 });

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