即將推出WCF Web API,有什麼方法可以控制JSON輸出嗎?有什麼辦法可以通過WCF Web API控制JSON格式?
我想改變套管,並可能抑制一些類被序列化時包含某些屬性。
舉個例子,認爲這很乾脆類:
[XmlRoot("catalog", Namespace = "http://api.247e.com/catalog/2012")]
public class Catalog
{
[XmlArray(ElementName = "link-templates")]
public LinkTemplate[] LinkTemplates { get; set; }
}
正如你所看到的,我已經添加了各種XML屬性,它爲了控制它是如何在XML序列化。我可以爲JSON做同樣的事情嗎?
作爲參考,這裏是在XML一個輸出樣本:
<catalog xmlns="http://api.247e.com/catalog/2012"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<link-templates>
<link-template href="http://localhost:9000/search/?criterion={criterion}"
rel="http://docs.247e.com/rels/search"/>
</link-templates>
</catalog>
對於JSON,等效結果是這樣的:
{
"LinkTemplates":
[
{
"Href":"http:\/\/localhost:9000\/search\/?criterion={criterion}",
"Rel":"http:\/\/docs.247e.com\/rels\/search"
}
]
}
然而,我想變更的屬性的殼體,所以我更喜歡這樣的事情,而不是:
{
"linkTemplates":
[
{
"href":"http:\/\/localhost:9000\/search\/?criterion={criterion}",
"rel":"http:\/\/docs.247e.com\/rels\/search"
}
]
}
一種方法來剝去某些類的屬性也將b很好。