【问题标题】:C# json custom serializingC# json 自定义序列化
【发布时间】:2018-04-30 06:59:30
【问题描述】:

我在 c# 中创建了一个自定义标签对象,我需要从这个对象中创建 json 对象。 但是我已经将标签从“标签”控件派生到自定义标签对象。 序列化自定义标签对象后,json 被标签属性填充。 但是,我不需要它。我只需要传递自定义标签对象。

这是自定义标签:

public class customLabel:Label
{

        public string X { get; set; }

        public string Y { get; set; }

        public string H { get; set; }

        public string W { get; set; }

        public string FontName { get; set; }

        public string FontSize { get; set; }

        public string Type { get; set; }

        public string Align { get; set; }

        public string _Text { get; set; }


}

我正在使用 Newtonsoft.Json 进行 json 序列化

【问题讨论】:

  • 你的问题是什么?
  • 我怎样才能只传递自定义标签对象进行序列化? @塞巴斯蒂安霍夫曼
  • 是的,但是你的对象继承自 Label 所以,它也是一个标签。
  • @Jodrell 我只能将此值传递给 json 吗?
  • @DanialDP this values 是什么意思?您可以将 any 对象传递给 JSON.NET。对象将始终被序列化为字典。你尝试了吗?你用了什么代码?你得到了什么,你期望什么?

标签: c# json


【解决方案1】:

创建一个包含所需属性的custom JsonConvertor

然后将其传递给SerializeObject 来控制序列化。

string result = JsonConvert.SerializeObject(
                     customLabel,
                     Formatting.Indented,
                     new CustomLabelConverter(typeof(CustomLabel)));

【讨论】:

    【解决方案2】:

    看看这个Ignore Base Class Properties in Json.NET Serialization

    [JsonObject(MemberSerialization.OptIn)]
    public class customLabel:Label
    {
        [JsonProperty("X")]
        public string X { get; set; }
    
        [JsonProperty("Y")]
        public string Y { get; set; }
    
        ...
        public string H { get; set; }
    
        public string W { get; set; }
    
        public string FontName { get; set; }
    
        public string FontSize { get; set; }
    
        public string Type { get; set; }
    
        public string Align { get; set; }
    
        public string _Text { get; set; }
    
    }
    

    但是你需要把 JsonProperty 放到你需要序列化它的所有属性中

    【讨论】:

      【解决方案3】:

      试试这样的:

      customLabel yourLabel = new customLabel();
      
      yourLabel.X = 50;
      yourLabel.Y = 20;
      //....
      
      string output = JsonConvert.SerializeObject(yourLabel);
      //output contains the serialized object
      
      customLabel deserializedLabel = JsonConvert.DeserializeObject<customLabel>(output);
      

      编辑: 将您的课程更改为:

      [DataContract]
      public class customLabel:Label
      {
           [DataMember]
           public string X { get; set; }
           [DataMember]    
           public string Y { get; set; }
           [DataMember]
           public string H { get; set; }
           [DataMember]    
           public string W { get; set; }
           [DataMember]
           public string FontName { get; set; }
           [DataMember]   
           public string FontSize { get; set; }
           [DataMember]  
           public string Type { get; set;
           [DataMember]
           public string Align { get; set; }
           [DataMember]
           public string _Text { get; set; }
      }
      

      现在应该只包含属性为 [DataMember] 的属性

      并查看文档:https://www.newtonsoft.com/json/help/html/SerializingJSON.htm

      【讨论】:

      • 但它的传递标签属性。我只想传递自定义标签值!
      • 对不起,我误解了你。我添加了类定义
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-16
      • 2011-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多