1

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;

   }

2 Answers 2

2

What about something like this...

public ActionResult GetCook()
{
    var cook = new Cook();
    return Json(new 
    { 
        atime = cook.time1, 
        anothertime = cook.time2, 
        yetanothertime = cook.time3 
    });
}
Sign up to request clarification or add additional context in comments.

2 Comments

I have some names with special characters where I can't have that as property names.
If you want a better answer, you might want to be more specific about what you are trying to get. In the end, you might need to just write out your own JSON. The format is pretty simple (json.org).
0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.