【问题标题】:Serializing Key/Value in WCF JSON序列化 WCF JSON 中的键/值
【发布时间】:2012-11-21 11:57:19
【问题描述】:

我有以下代码

[DataContract]
    public class DeviceParam 
    {
        public long ID { get; set; }
        [DataMember]
        public string Key { get; set; }
        [DataMember(EmitDefaultValue = false)]
        public string Value { get; set; }
        [DataMember(Name = "param", EmitDefaultValue = false)]
        public List<DeviceParam> Param { get; set; }
    }

问题是,它产生了:

{"Key":"Param1","param":[{"Key":"ParamChild1","Value":"1"}]}]

我正在努力实现所需的格式:

{"Param1": {"ParamChild1" : "1" }}

我无法做到这一点,因为该类包含自身的类。并且似乎 WCF 不喜欢我为实现它所做的所有修改。

使用 WCF 输出 JSON 实现这一点的最佳方法是什么?

谢谢!

【问题讨论】:

标签: .net json wcf


【解决方案1】:

按如下方式划分你的班级:

[DataContract]
    public class DeviceParam 
    {
        public long ID { get; set; }
        [DataMember]
        public string Key { get; set; }
        [DataMember(EmitDefaultValue = false)]
        public string Value { get; set; }
    }
 [DataContract]
  public class DeviceParamsList
  {
        [DataMember(Name = "param", EmitDefaultValue = false)]
        public List<DeviceParam> Param { get; set; }
  }

没试过,应该可以的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-01
    • 2021-11-13
    相关资源
    最近更新 更多