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 8513689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:38:54+00:00 2026-06-11T04:38:54+00:00

I have the following code in a controller. class Person { public string Name

  • 0

I have the following code in a controller.

        class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public DateTime Birthday { get; set; }
    }

    public ActionResult JsonObject()
    {
        Person[] persons = new Person[]{
            new Person{Name="John", Age=26, Birthday=new DateTime(1986,1,1)},
            new Person{Name="Tom", Age=10, Birthday=new DateTime(2002, 1, 9)}
        };

        return Json(persons, JsonRequestBehavior.AllowGet);
    }

Normally, I got the result like this:
[{“Name”:”John”,”Age”:26,”Birthday”:”/Date(504892800000)/”},{“Name”:”Tom”,”Age”:10,”Birthday”:”/Date(1010505600000)/”}]

That’s okay, however, I want to make an option for the user: not to display birthday. So, the expecting result would be like this:
[{“Name”:”John”,”Age”:26},{“Name”:”Tom”,”Age”:10}]

How can I not to serialize the Birthday property to JSON?

  • 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-06-11T04:38:56+00:00Added an answer on June 11, 2026 at 4:38 am

    You have two options:

    1) Add a [ScriptIgnore] attribute to the Person class:

    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
        [ScriptIgnore]
        public DateTime Birthday { get; set; }
    }
    

    2) Return an anonymous type that contains only the properties you want:

    var toReturn = persons.Select(x => new {x.Name, x.Age});
    return Json(toReturn, JsonRequestBehavior.AllowGet);
    

    EDIT: I wasn’t aware that the desired columns had to be dynamically chosen. You can use the following because objects and dictionaries are the same thing in Javascript.

    First, create an extension that creates a dictionary of your desired properties:

    public static class JSONExtensions
    {
        public static IDictionary<string, object> ToJsonObject(this object instance, string[] includedProperties)
        {
            var jsonObject = new Dictionary<string, object>();
            foreach (var property in instance.GetType().GetProperties())
            {
                if (includedProperties.Any(x=> x.Equals(property.Name, StringComparison.InvariantCultureIgnoreCase)))
                {
                    jsonObject[property.Name] = property.GetValue(instance);
                }
            }
            return jsonObject;
        }
    }
    

    Next, use this extension method before serializing:

    var toReturn = persons.Select(x => x.ToJsonObject(desiredColumnss));
    return Json(toReturn, JsonRequestBehavior.AllowGet);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say i have the following: public class Person{ public int ID {get; set;} public
I have a controller class as in the following code segment: @Controller public class
I have a controller with the following action code: public JsonResult CheckPasswordStrength(string password, string
I have written the following code : @Controller @RequestMapping(/test) public class Home { @RequestMapping(value
Hi I have the following code: Controller: @Controller public class HelloWorldController { @RequestMapping(/hello) public
I have the following code public class BaseController : Controller { SqlConnection con; DataSet
Inside my controller's action I have the following code: public ActionResult GridAction(string id) {
I have following code in controller: public ActionResult Index(string date) { var topsong =
I have the following code: class Controller { public Controller(Listener audioListener, Listener videoListener) {}
I have the following code in my controller: // GET: /Review/Create public ActionResult Create()

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.