Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3623982
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:27:03+00:00 2026-05-18T23:27:03+00:00

I am creating a small number of JSON Web Services using JsonResult as part

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T23:27:04+00:00Added an answer on May 18, 2026 at 11:27 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.