简体   繁体   English

C#JSON自定义序列化

[英]C# json custom serializing

I have created a custom label object in c# and I need to make json object from this object. 我已经在c#中创建了一个自定义标签对象,并且需要从该对象制作json对象。 But I have derived the Label from the 'Label' control to custom label object. 但是我已经将标签从“标签”控件派生到自定义标签对象。 After serializing custom label object, json is getting filled with label properties. 序列化自定义标签对象后,json将被标签属性填充。 But, I don't need it. 但是,我不需要它。 I need to pass only the custom label object. 我只需要传递自定义标签对象。

This is custom label : 这是自定义标签:

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; }


}

I am using Newtonsoft.Json for json serializng 我正在使用Newtonsoft.Json进行json serializng

Create a custom JsonConvertor that includes the properties you want. 创建一个包含所需属性的自定义JsonConvertor

Then pass it to SerializeObject to control the serialization. 然后将其传递给SerializeObject以控制序列化。

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

Take a look at this Ignore Base Class Properties in Json.NET Serialization 看看Json.NET序列化中的忽略基类属性

[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; }

}

but you need to put JsonProperty to all any property you need to serialize it 但是您需要将JsonProperty置于序列化所需的所有属性中

Try something like this: 尝试这样的事情:

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);

edit: Change your class to this: 编辑:将您的班级更改为此:

[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; }
}

Now only the properties with the attribute [DataMember] should be included 现在,仅应包含属性为[DataMember]的属性

And take a look at the documentation: https://www.newtonsoft.com/json/help/html/SerializingJSON.htm 并查看文档: https : //www.newtonsoft.com/json/help/html/SerializingJSON.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM