Background today in use: C # Json serialization and deserialization
The following error message appears when deserializing.
System.Runtime.Serialization.SerializationException: Data contract type ' testentity ' cannot be deserialized because the required data member ' Multiplechoice is not found, Runtimedisplaycolumns ". In System.ComponentModel.ReflectPropertyDescriptor.SetValue (object component, Object value)
The reason for this is that I have added two new attributes to the deserialized entity class:
private bool Multiplechoice; <summary> ///Whether the Help engine allows multiple selections ///</summary> [XmlIgnore] [Browsable (false)] public BOOL Multiplechoice { get { return multiplechoice; } Set { multiplechoice = value; } } private string runtimedisplaycolumns; <summary> ///The column displayed when the engine is running ///</summary> [XmlIgnore] [Browsable (false)] public string Runtimedisplaycolumns { get { return runtimedisplaycolumns; } Set { runtimedisplaycolumns = value; } }
When using a previously saved JSON string to deserialize, the new two two properties, no corresponding value, so reported the above error.
Workaround:
[datacontract]public class testentity{private bool Multiplechoice; <summary>///Whether the Help engine allows multiple selections///</summary> [XmlIgnore] [browsable (false)][da Tamember (isrequired = false)] public bool Multiplechoice {get {return Multiplechoice; } set {multiplechoice = value; }} private string Runtimedisplaycolumns; <summary>///The column that is displayed when the engine runs///</summary> [XmlIgnore] [browsable (false)][d Atamember (isrequired = False)] public string Runtimedisplaycolumns {get { return runtimedisplaycolumns; } set {runtimedisplaycolumns = value; } }}
This property is not required through the DataMember (IsRequired = False) property surface. This ensures that when deserializing, the definition of the missing attribute in the JSON string is normally deserialized.
Small bet
after adding DataMember (IsRequired = False) on the property, the [DataContract] identity must be added on the corresponding class.
Above is the above is the content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)! For more information, please pay attention to topic.alibabacloud.com (www.php.cn)!