如何从WCF REST方法将自定义类型值的Dictionary作为常规JSON对象返回? [英] How to return a Dictionary of custom type values as a regular JSON object from a WCF REST method?

查看:89
本文介绍了如何从WCF REST方法将自定义类型值的Dictionary作为常规JSON对象返回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个自定义类型,如下所示:

Let's say I've got a custom type that looks like this:

[DataContract]
public class CompositeType
{
    [DataMember]
    public bool HasPaid
    {
        get;
        set;
    }

    [DataMember]
    public string Owner
    {
        get;
        set;
    }
}

和如下所示的WCF REST接口:

and a WCF REST interface that looks like this:

[ServiceContract]
public interface IService1
{
    [OperationContract]
    Dictionary<string, CompositeType> GetDict();
}

然后我该如何实现该方法以返回一个看起来像这样的JSON对象...

then how do I get my implementation of that method to return a JSON object that looks like this...

{"fred":{"HasPaid":false,"Owner":"Fred Millhouse"},"joe":{"HasPaid":false,"Owner":"Joe McWirter"},"bob":{"HasPaid":true,"Owner":"Bob Smith"}}

希望它看起来像这样:

I do not want it to look like this:

[{"Key":"fred","Value":{"HasPaid":false,"Owner":"Fred Millhouse"}},{"Key":"joe","Value":{"HasPaid":false,"Owner":"Joe McWirter"}},{"Key":"bob","Value":{"HasPaid":true,"Owner":"Bob Smith"}}]

理想情况下,我希望不必更改方法的返回类型.

Ideally I would prefer not to have to alter the return type of the method.

我尝试了许多不同的方法,但是找不到可行的解决方案.令人烦恼的是,很容易用Newtonsoft.Json在同一行中生成正确形状的JSON对象结构:

I have tried many different approaches but cannot find a solution that works. Annoyingly, it is easy to produce the right-shaped JSON object structure in one line with Newtonsoft.Json:

string json = JsonConvert.SerializeObject(dict);

其中dict定义为:

Dictionary<string, CompositeType> dict = new Dictionary<string, CompositeType>();
dict.Add("fred", new CompositeType { HasPaid = false, Owner = "Fred Millhouse" });
dict.Add("joe", new CompositeType { HasPaid = false, Owner = "Joe McWirter" });
dict.Add("bob", new CompositeType { HasPaid = true, Owner = "Bob Smith" });

但是我不想从WCF方法返回字符串.这是因为它隐藏了返回的实类型;也是因为WCF还会对字符串进行序列化,从而导致转义的双引号和其他丑陋之处,使得非.Net REST客户端难以解析.

but I do not want to return a string from my WCF method. This is because it conceals the real type being returned; and also because WCF serializes the string as well, resulting in escaped double quotes and other ugliness that makes it harder for non-.Net REST clients to parse.

推荐答案

这是对@dbc的评论的部分解决方案.结果是该结构的右形JSON结构...

This is a partial solution in response to comments by @dbc. It results in the right-shaped JSON structure of this...

{"fred":{"HasPaid":false,"Owner":"Fred Millhouse"},"joe":{"HasPaid":false,"Owner":"Joe McWirter"},"bob":{"HasPaid":true,"Owner":"Bob Smith"}}

但不幸的是,必须将方法的返回类型更改为

but unfortunately involves having to change the return type of the method to Message. The interface becomes:

[ServiceContract]
public interface IService1
{
    [OperationContract]
    Message GetDict();
}

,实现变为:

using Newtonsoft.Json;
...
[WebGet(ResponseFormat = WebMessageFormat.Json)]
public Message GetDict()
{
    Dictionary<string, CompositeType> dict = new Dictionary<string, CompositeType>();
    dict.Add("fred", new CompositeType { HasPaid = false, Owner = "Fred Millhouse" });
    dict.Add("joe", new CompositeType { HasPaid = false, Owner = "Joe McWirter" });
    dict.Add("bob", new CompositeType { HasPaid = true, Owner = "Bob Smith" });

    string json = JsonConvert.SerializeObject(dict);
    return WebOperationContext.Current.CreateTextResponse(json, "application/json; charset=utf-8", Encoding.UTF8);
}

要注意的一个有用功能是,与返回 Stream ,当您访问REST方法的URI时,您可以在Web浏览器中轻松查看JSON.

One useful feature to note is that, unlike when returning Stream, you can view the JSON easily in your web browser when you visit the REST method's URI.

这篇关于如何从WCF REST方法将自定义类型值的Dictionary作为常规JSON对象返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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