从ApiController JSON序列排除属性 [英] Exclude property from json serialization in ApiController

查看:209
本文介绍了从ApiController JSON序列排除属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从正在连载到JSON的Web ApiControllers排除特性。我已经验证了以下两种情景工作。

我已经列入我希望排除属性以下属性。

  [System.Web.Script.Serialization.ScriptIgnore]
[System.Xml.Serialization.XmlIgnore]

如果我手动序列化使用的JavaScriptSerializer我的对象,属性被排除在外。另外,如果我认为从网上ApiController XML输出​​序列化,财产被排除在外。问题是,通过网络ApiController序列化的JSON仍包含属性。有没有我可以使用,这将排除JSON序列化属性的另一个属性?

更新:

我意识到我所有的测试都是在一个更复杂的项目,我没有在一个孤立的环境中尝试这样做。我这样做,和我仍然得到同样的结果。下面是一些code,它是失败的例子。

 公共类Person
{
    公共字符串名字{获得;组; }    [System.Web.Script.Serialization.ScriptIgnore]
    [System.Xml.Serialization.XmlIgnore]
    公共字符串名字{获得;组; }
}公共类把PeopleController:ApiController
{
    公共IEnumerable的<&人GT;得到()
    {
        返回新的[]
                   {
                       新的Person {名字=约翰,姓氏=李四},
                       新的Person {名字=简,姓氏=李四}
                   };
    }
}

下面是生成的输出。

JSON:

  [
    {
        名字:约翰,
        名字:李四
    },
    {
        名字:珍,
        名字:李四
    }
]

XML:

 <?XML版本=1.0编码=UTF-8&GT?;
< ArrayOfPerson的xmlns:XSI =htt​​p://www.w3.org/2001/XMLSchema-instance的xmlns:XSD =htt​​p://www.w3.org/2001/XMLSchema>
    <&人GT;
        <&名字GT;约翰和LT; /姓>
    < /人>
    <&人GT;
        <&名字GT;简< /姓>
    < /人>
< / ArrayOfPerson>


解决方案

请注意,JSON序列化是Web API的变化。

在测试版发布,Web API使用DataContractJsonSerializer序列化JSON。然而,这种情况已经改变; 最新的Web API使用的版本的 json.net 通过默认情况下,虽然您可以覆盖此并使用DataContractJsonSerializer来代替。

使用DataContractJsonSerializer,您可以使用[DataContract]属性来控制序列化。我还是老样子不是很熟悉json.net,所以我不知道它是如何控制序列化。

I am trying to exclude properties from being serialized to JSON in web ApiControllers. I have verified the following 2 scenarios work.

I have included the following attributes on the property I wish to exclude.

[System.Web.Script.Serialization.ScriptIgnore]
[System.Xml.Serialization.XmlIgnore]

If I manually serialize my object using the JavaScriptSerializer, the property is excluded. Also, if I view the serialized XML output from the web ApiController, the property is excluded. The problem is, the serialized JSON via the web ApiController still contains the property. Is there another attribute that I can use that will exclude the property from JSON serialization?

UPDATE:

I realized all my tests were in a much more complex project and that I hadn't tried this in a an isolated environment. I did this and am still getting the same results. Here is an example of some code that is failing.

public class Person
{
    public string FirstName { get; set; }

    [System.Web.Script.Serialization.ScriptIgnore]
    [System.Xml.Serialization.XmlIgnore]
    public string LastName { get; set; }
}

public class PeopleController : ApiController
{
    public IEnumerable<Person> Get()
    {
        return new[]
                   {
                       new Person{FirstName = "John", LastName = "Doe"},
                       new Person{FirstName = "Jane", LastName = "Doe"}
                   };
    } 
}

Here is the generated output.

JSON:

[
    {
        "FirstName" : "John",
        "LastName" : "Doe"
    },
    {
        "FirstName" : "Jane",
        "LastName" : "Doe"
    }
]

XML:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfPerson xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Person>
        <FirstName>John</FirstName>
    </Person>
    <Person>
        <FirstName>Jane</FirstName>
    </Person>
</ArrayOfPerson>

解决方案

Be aware that JSON serialization is changing in Web API.

In the beta release, Web API used DataContractJsonSerializer to serialize JSON. However, this has changed; the latest version of Web API uses json.net by default, although you can override this and use DataContractJsonSerializer instead.

With DataContractJsonSerializer, you can use [DataContract] attributes to control the serialization. I'm stil not very familiar with json.net, so I don't know how it controls serialization.

这篇关于从ApiController JSON序列排除属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆