1

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?

2 Answers 2

3

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);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. But the option means this behavior can be changed in runtime. Consider this: User like to see a table and he can decide which columns to display and which to hide.
That shouldn't be done by changing the JSON, then. It should be done by hiding the column.
@jgg I wasn't aware that the columns had to be dynamic. I've updated my answer with another approach.
0

just define a new class as DTO, which contains only required fields.

public class PersonDTO
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }

After that, you can PersonDTO

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.