【问题标题】:How to re-serialize a data structure from a service and keep the same field names?如何从服务重新序列化数据结构并保持相同的字段名称?
【发布时间】:2011-07-20 07:56:36
【问题描述】:

我正在编写一个中间层 WCF 服务,它需要从后端 .asmx 服务获取一些数据,然后将其作为 JSON 编码字符串提供给前端。

我通过向我的项目添加对后端服务的引用来实现此目的,使用该引用获取数据,然后使用 DataContractJsonSerializer 重新序列化数据,如下所示:

            using (BackendService.BackendServicesSoapClient c = new BackendService.BackendServicesSoapClient())
            {
                // Get data from back-end
                BackendService.SomeData data = c.GetData();

                using (MemoryStream msData = new MemoryStream())
                {
                        // Serialise data to a JSON-encoded string
                        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(BackendService.SomeData));
                        serializer.WriteObject(msData, data);
                        jsonEncodedData = System.Text.Encoding.UTF8.GetString(msData.GetBuffer(), 0, Convert.ToInt16(msData.Length));
                }
            }

现在,总的来说这是可行的。但是,JSON 编码字符串中的字段名称与原始 SomeData 类中的字段名称不匹配 - 它们都附加了“字段”。

例如

contents of BackendService.Somedata:
    Name  : Joe
    Age   : 33

contents of JSON-encoded string
    nameField : Joe
    ageField : 33

这似乎与我将服务引用添加到我的项目时自动生成的代理代码有关。如果我查看生成的reference.cs,我会看到数据类的定义如下:

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.225")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.com/Data/BackEnd/2010/11/")]
public partial class SomeData : object, System.ComponentModel.INotifyPropertyChanged {

    private string nameField;

    private string locationNameField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order=0)]
    public string Name {
        get {
            return this.nameField;
        }
        set {
            this.nameField = value;
            this.RaisePropertyChanged("Name");
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order=1)]
    public string Age {
        get {
            return this.ageField;
        }
        set {
            this.ageField = value;
            this.RaisePropertyChanged("Age");
        }
    }
}

与后端服务源中定义的类相比:

public class SomeData
{
    public string Name { get; set; }
    public string Age { get; set; }
}

有什么方法可以让前端和后端的字段名称匹配?

【问题讨论】:

    标签: wcf json serialization service asmx


    【解决方案1】:

    问题是您在生成代理时使用了 XML 序列化器,然后尝试使用 DataContract(Json)Serializer 重新序列化。这些使用不同的属性。您的类没有使用 DataContract 属性进行注释,而是使用 Serializable 属性进行注释

    为此,您需要代理类上的 DataContract 属性,或者您需要删除 Serializable 属性。不幸的是,当您使用 XmlSerialier 时,我不确定如何让生成的代码为您执行这些操作(因为下游服务是 .ASMX 服务)。

    为了您自己的理智,创建您自己的类来创建您需要的 JSON,将来自 ASMX 服务的数据映射到此类,然后使用您的类序列化为 JSON,这可能是一个好主意。这有一个额外的好处,如果 asmx 服务由于某种原因发生了变化,它不会影响 JSON,除非你想要它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-30
      • 1970-01-01
      • 1970-01-01
      • 2021-06-07
      • 2017-11-10
      • 2014-10-29
      相关资源
      最近更新 更多