开发者

Change the JSON Key Text in a MVC2 controller which returns JsonResult

开发者 https://www.devze.com 2023-02-01 06:00 出处:网络
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

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.


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 );
0

精彩评论

暂无评论...
验证码 换一张
取 消