pili
pili

Reputation: 825

asp.net mvc json serialization attributes

How to change the name of the field names in Json output I want the Class name cook and field "time1" to be something else please. I am using asp.net mvc controller class to return the json result.

public class Cook
    {
        public string time1;
        public string time2;
        public string time3;

   }

Upvotes: 1

Views: 1349

Answers (2)

J. Salman
J. Salman

Reputation: 61

You can write your custom Attribute and access that value through ViewData.ModelMetadata (make sure that your attribute class implements IMetadataAware). Then create your own JSON class which puts all your properties and its values inside a dictionary. The key for each property will be the name that you specify in your custom attribute.

This sounds a little cryptical maybe, but if you think you like this approuch I can give you a code example. Good luck!

Upvotes: 0

Brian
Brian

Reputation: 38025

What about something like this...

public ActionResult GetCook()
{
    var cook = new Cook();
    return Json(new 
    { 
        atime = cook.time1, 
        anothertime = cook.time2, 
        yetanothertime = cook.time3 
    });
}

Upvotes: 2

Related Questions