WCF服务将JSON解析为Dictionary< string,string>() [英] WCF Service parse JSON as Dictionary<string,string>()

查看:244
本文介绍了WCF服务将JSON解析为Dictionary< string,string>()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一整套WCF Web服务,它们接收POST JSON对象并用其他JSON格式的数据进行回复.

I have a whole load of WCF web services that receive POST JSON objects and reply with other JSON formatted data.

在服务中的一种特定方法中,我试图传递JSON对象并将其解析为Dictionary().

In one particular method within a service I'm trying to pass a JSON object through and parse it as a Dictionary().

该方法的接口定义为:

[OperationContract(Name = "GetSomeData")]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
string GetSomeData(AuthenticationData authData, Dictionary<string, string> options, string srcHash);

和方法声明本身是:

public string GetSomeData(AuthenticationData authData, Dictionary<string,string> options, string srcHash)
{
    // do something fancy
}

authData和srcHash是所有方法上的标准参数,并且包含正确解析为authData的AuthenticationData对象的预期数据.

the authData, and srcHash are standard parameters on all the methods and contain the expected data, correctly parsed as an AuthenticationData object for authData.

其他方法对于声明的对象和基元都可以很好地工作,但是Dictionary始终为空.

Other methods work perfectly fine for both declared objects and primitives however the Dictionary is always empty.

通过发送的JSON字符串是:

The JSON string being sent through is:

"{\"options\",{\"id\":\"1\"}}"

为什么不将其解析为字典?

Why is this not being parsed as a Dictionary?

推荐答案

我最终放弃了这一点,创建了一个List类来模拟字典.

I eventually gave up on this and created a List class to simulate a dictionary.

[Serializable]
public class JSONDictionary
{
    [DataMember] public string _key;
    [DataMember] public string _value;
}

然后在客户端(Javascript):

Then on the client side (Javascript):

client.utils.toJSONDictionary = function (obj)
{
    var dictionary = [];
    for (member in obj)
    {
        dictionary.push({ _key: member, _value: obj[member].toString() });
    }
    return dictionary;
}

那很好.在c#中使用适当的字典会很好,但是...嗯,您不可能拥有一切.

And that works fine. Would be nice to use a proper dictionary in c# but... well you can't have everything.

这篇关于WCF服务将JSON解析为Dictionary&lt; string,string&gt;()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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