C # JSON deserialization data contract type cannot be deserialized because a required data member was not found

Source: Internet
Author: User
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)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.