删除发送到Json MVC的对象的null属性

时间:2014-01-07 14:56:37

标签: c# json serialization asp.net-mvc-5

namespace Booking.Areas.Golfy.Models
{
    public class Time
    {
        public string   time            { get; set; }
        public int      holes           { get; set; }
        public int      slots_available { get; set; }
        public decimal? price           { get; set; }
        public int?     Nextcourseid    { get; set; }

        public bool ShouldSerializeNextcourseid
        {
            get
            {
                return this.Nextcourseid != null;
            }
        }

        public bool? allow_extra { get; set; }

        public bool ShouldSerializeallow_extra
        {
            get
            {
                return this.allow_extra != null;
            }
        }
    }
}


namespace Booking.Areas.Golfy.Controllers
{
    public class TeetimesController : Controller
    {
        //
        // GET: /Golfy/Teetimes/
        public ActionResult Index(
            string date,
            int?   courseid = null,
            int?   clubid = null,
            int?   holes = null,
            int?   available = null,
            int?   prices = null
        )
        {
            var DateFrom = DateTime.ParseExact(date, "yyyy-MM-dd", CultureInfo.InvariantCulture);

            Teetimes r = BookingManager.GetBookings(new Code.Classes.Params.ParamGetBooking()
            {
                ClubID = clubid
                , DateFrom = DateFrom
                , DateTo = DateFrom.AddDays(1)
                , GroundID = courseid
            });

            return Json(r, JsonRequestBehavior.AllowGet);
        }
    }
}

上面的webservice返回一个json字符串,其中包含几个Time of Time。

我希望属性Nextcourseid和allow_extra在其值为null时不在输出中。

我尝试过ShouldSerializeXxx,但它似乎不起作用。
仅供参考:我也尝试过[ScriptIgnore],但不是有条件的。

3 个答案:

答案 0 :(得分:12)

您无法使用默认Json ActionResult删除null属性。

您可以查看JSON.NET,它有一个属性可以设置为删除属性为null

[JsonProperty(NullValueHandling=NullValueHandling.Ignore)]

或者,如果您不想使用其他库,您可以创建自己的json自定义ActionResult并为默认JavaScriptSerializer注册一个新的转换器,如下所示:

public class JsonWithoutNullPropertiesResult : ActionResult
{
    private object Data { get; set; }

    public JsonWithoutNullPropertiesResult(object data)
    {
        Data = data;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        HttpResponseBase response = context.HttpContext.Response;
        response.ContentType = "application/x-javascript";
        response.ContentEncoding = Encoding.UTF8;

        if (Data != null)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            serializer.RegisterConverters(new[] { new NullPropertiesConverter() });
            string ser = serializer.Serialize(Data);
            response.Write(ser);
        }
    }
}

public class NullPropertiesConverter : JavaScriptConverter
{
    public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
    {
        var toSerialize = new Dictionary<string, object>();

        foreach (var prop in obj.GetType()
                                .GetProperties(BindingFlags.Instance | BindingFlags.Public)
                                .Select(p => new
                                {
                                    Name = p.Name,
                                    Value = p.GetValue(obj)
                                })
                                .Where(p => p.Value != null))
        {
            toSerialize.Add(prop.Name, prop.Value);
        }

        return toSerialize;
    }

    public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
    {
        throw new NotImplementedException();
    }

    public override IEnumerable<Type> SupportedTypes
    {
        get { return GetType().Assembly.GetTypes(); }
    }
}

现在在你看来:

public ActionResult Index()
{
    Teetimes r = BookingManager.GetBookings();
    return new JsonWithoutNullPropertiesResult(t);
}

答案 1 :(得分:2)

我总是遇到嵌入框架的json序列化程序的问题,因此我使用Json.NET。这是测试这两个序列化器的小例子:

public class Model {
    public int Id { get; set; }
    public string Name { get; set; }

    public bool ShouldSerializeName() {
        return Name != null;
    }
}

class Program {
    static void Main(string[] args) {
        var t1 = new Model {
            Name = "apw8u3rdmapw3urdm",
            Id = 298384
        };
        var t2 = new Model {
            Id = 234235
        };

        Test(t1);
        Test(t2);
    }

    static void Test(Model model) {
        Console.WriteLine("JSon from .Net: {0}", ToJson(model));
        Console.WriteLine("JSon from JSon.Net: {0}", ToDotNetJson(model));
    }

    static string ToJson(Model model) {
        var s = new JavaScriptSerializer();
        return s.Serialize(model);
    }

    static string ToDotNetJson(Model model) {
        return JsonConvert.SerializeObject(model);
    }
}

您必须包含System.Web.Extensions作为依赖项,并使用nuget安装Json.Net以使示例正常运行。

以下是Json.NETFramework-embedded serializer

的一些文档

答案 2 :(得分:2)

给出的答案是有趣的,但我在此期间开始使用 DataContractJsonSerializer
它可以在不需要使用第三方框架的情况下完成工作(尽管JSON.Net似乎广泛使用)。

public ActionResult Index(
    string date
    , int? courseid = null
    , int? clubid = null
    , int? holes = null
    , int? available = null
    , int? prices = null
)
{
    var DateFrom = DateTime.ParseExact(date, "yyyy-MM-dd", CultureInfo.InvariantCulture);

    MTeetimes r = BookingManager.GetBookings(new Code.Classes.Params.ParamGetBooking()
    {
        ClubID = clubid
        , DateFrom = DateFrom
        , DateTo = DateFrom.AddDays(1)
        , GroundID = courseid
    });

    // return Json(r, JsonRequestBehavior.AllowGet);

    string response;
    var serializer = new DataContractJsonSerializer(typeof(MTeetimes));

    // Serialize
    using (var ms = new MemoryStream())
    {
        serializer.WriteObject(ms, r);
        response = Encoding.Default.GetString(ms.ToArray());
    }

    return Content(response);
}


[DataContract]
public class Time
{
    [DataMember(Name="time", EmitDefaultValue = false)]
    public string Time
    {
        get;
        set;
    }

    [DataMember(Name = "holes", EmitDefaultValue = false)]
    public int Holes
    {
        get;
        set;
    }

    [DataMember(Name = "slots_available", EmitDefaultValue = false)]
    public int Slots_available
    {
        get;
        set;
    }

    [DataMember(Name = "price", EmitDefaultValue = false)]
    public decimal? Price
    {
        get;
        set;
    }

    [DataMember(Name = "nextcourseid", EmitDefaultValue = false)]
    public int? Nextcourseid
    {
        get;
        set;
    }

    [DataMember(Name = "allow_extra", EmitDefaultValue = false)]
    public bool? Allow_extra
    {
        get;
        set;
    }
}