通用 WCF JSON 反序列化 [英] Generic WCF JSON Deserialization

查看:25
本文介绍了通用 WCF JSON 反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 WCF 有点陌生,我会尽量清楚地描述我想要做什么.

I am a bit new to WCF and will try to clearly describe what I am trying to do.

我有一个使用 JSON 请求的 WCF Web 服务.我在大部分情况下都可以很好地发送/接收 JSON.例如,以下代码运行良好且符合预期.

I have a WCF webservice that uses JSON requests. I am doing fine sending/receiving JSON for the most part. For example, the following code works well and as expected.

JSON 发送:

{ "guy": {"FirstName":"Dave"} }

WCF:

    [DataContract]
    public class SomeGuy
    {
        [DataMember]
        public string FirstName { get; set; }
    }

    [OperationContract]
    [WebInvoke(Method = "POST",
               BodyStyle = WebMessageBodyStyle.WrappedRequest,
               RequestFormat = WebMessageFormat.Json,
               ResponseFormat = WebMessageFormat.Json)]
    public string Register(SomeGuy guy)
    {
        return guy.FirstName;
    }

这将按预期返回带有Dave"的 JSON 对象.问题是我不能总是保证我收到的 JSON 与我的 DataContract 中的成员完全匹配.例如,JSON:

This returns a JSON object with "Dave" as expected. The problem is that I cannot always guarantee that the JSON I recieve will exactly match the members in my DataContract. For example, the JSON:

{ "guy": {"firstname":"Dave"} }

不会正确序列化,因为大小写不匹配.Guy.FirstName 将为空.这种行为是有道理的,但我真的不知道如何解决这个问题.我是否必须在客户端强制使用字段名称,或者有什么方法可以在服务器端进行协调?

will not serialize correctly because the case does not match. guy.FirstName will be null. This behavior makes sense, but I don't really know how to get around this. Do I have to force the field names on the client or is there a way I can reconcile on the server side?

一个可能相关的问题:我可以接受一个通用的 JSON 对象并将其序列化为 StringDictionary 或某种简单的键值结构吗?那么无论JSON中发送的字段名称是什么,我都可以访问已发送给我的名称和值?现在,我可以读取收到的数据的唯一方法是它是否与预定义的 DataContract 完全匹配.

A possibly related question: can I accept and serialize a generic JSON object into a StringDictionary or some kind of simple key value structure? So no matter what the field names are sent in the JSON, I can access names and values that have been sent to me? Right now, the only way I can read the data I'm receiving is if it exactly matches a predefined DataContract.

推荐答案

这是将 json 读入字典的另一种方法:

Here's an alternative way to read json into dictionary:

[DataContract]
public class Contract
    {
    [DataMember]
    public JsonDictionary Registration { get; set; }
    }

[Serializable]
public class JsonDictionary : ISerializable
    {
    private Dictionary<string, object> m_entries;

    public JsonDictionary()
        {
        m_entries = new Dictionary<string, object>();
        }

    public IEnumerable<KeyValuePair<string, object>> Entries
        {
        get { return m_entries; }
        }

    protected JsonDictionary(SerializationInfo info, StreamingContext context)
        {
        m_entries = new Dictionary<string, object>();
        foreach (var entry in info)
            {
            m_entries.Add(entry.Name, entry.Value);
            }
        }

    public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
        foreach (var entry in m_entries)
            {
            info.AddValue(entry.Key, entry.Value);
            }
        }
    }

这篇关于通用 WCF JSON 反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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