有没有办法使用 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"
}
]
}
A way to strip away certain class属性也会不错。
With the upcoming WCF Web API, is there any way to control JSON output?
I'd like to change the casing and perhaps suppress that certain properties are included when a class is being serialized.
As an example, consider this very simply class:
[XmlRoot("catalog", Namespace = "http://api.247e.com/catalog/2012")]
public class Catalog
{
[XmlArray(ElementName = "link-templates")]
public LinkTemplate[] LinkTemplates { get; set; }
}
As you can see, I've added various XML attributes to it in order to control how it's serialized in XML. Can I do the same (or something else) for JSON?
For reference, here's a sample output in 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>
For JSON, the equivalent result is this:
{
"LinkTemplates":
[
{
"Href":"http:\/\/localhost:9000\/search\/?criterion={criterion}",
"Rel":"http:\/\/docs.247e.com\/rels\/search"
}
]
}
However, I'd like to change the casing of the properties, so I'd prefer something like this instead:
{
"linkTemplates":
[
{
"href":"http:\/\/localhost:9000\/search\/?criterion={criterion}",
"rel":"http:\/\/docs.247e.com\/rels\/search"
}
]
}
A way to strip away certain class properties would also be nice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
WCF Web API 默认使用 DataContractJsonSerializer 返回 JSON 格式的资源。因此,您应该在类上使用 DataContract 和 DataMember 属性来塑造 JSON 结果。
The WCF Web API used the DataContractJsonSerializer by default to return a resource in a JSON format. So you should be using the DataContract and DataMember attributes on you class to shape the JSON result.
尝试使用 codeplex 上 WCF Web API Contrib 项目中的 JSON.NET Formatter。此处显示的属性应该可以帮助您 https://json.svn.codeplex。 com/svn/trunk/Doc/SerializationAttributes.html
Try using the JSON.NET Formatter from the WCF Web API Contrib project on codeplex. Attributes like shown here should help you https://json.svn.codeplex.com/svn/trunk/Doc/SerializationAttributes.html
JSON.NET 有几个属性选项,可以控制序列化的内容和方式。 http://james.newtonking.com/projects/json/help/
JSON.NET has several attribute options which can control what is serialized and how. http://james.newtonking.com/projects/json/help/