有没有办法使用 WCF Web API 控制 JSON 格式?

发布于 2025-01-04 05:40:02 字数 1407 浏览 5 评论 0原文

随着即将推出的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冰魂雪魄 2025-01-11 05:40:02

WCF Web API 默认使用 DataContractJsonSerializer 返回 JSON 格式的资源。因此,您应该在类上使用 DataContract 和 DataMember 属性来塑造 JSON 结果。

[DataContract]
public class Book
{
    [DataMember(Name = "id")]
    public int Id { get; set; }
    [DataMember(Name = "title")]
    public string Title { get; set; }
    [DataMember(Name = "author")]
    public string Author { get; set; }
    [XmlIgnore] // Don't send this one
    public string ImageName { get; set; }
}

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.

[DataContract]
public class Book
{
    [DataMember(Name = "id")]
    public int Id { get; set; }
    [DataMember(Name = "title")]
    public string Title { get; set; }
    [DataMember(Name = "author")]
    public string Author { get; set; }
    [XmlIgnore] // Don't send this one
    public string ImageName { get; set; }
}
浅唱々樱花落 2025-01-11 05:40:02

尝试使用 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

生来就爱笑 2025-01-11 05:40:02

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文