error deserializing the object of type .... End element '......' from namespace '' expected. Found element 'item' from namespace ''
Asked Answered
S

2

8

When I deserialize my jsonstring ,I am getting error message

 There was an error deserializing the object of type RecordInfo. End element 'Warning' from namespace '' expected. Found element 'item' from namespace ''.

This is my JsonString

public const string jsonString = @" 
        {
            ""RequestId"":514106,
            ""Warning"":[],
            ""CustomerData"": {
                ""Email"":""[email protected]"",
                ""FullName"":""OrTguOfE"",
                ""OrderData"":[]
            }
        }";

Data contracts

[DataContract]
    public class RecordInfo
    {
        [DataMember(Name = "RequestId")]
        public string RequestId { get; set; }

        [DataMember(Name = "Warning")]
        public string Warning { get; set; }

        [DataMember(Name = "CustomerData")]
        public CustomerData CustomerData { get; set; }
    }
 [DataContract]
    public class CustomerData
    {
        [DataMember(Name = "Email")]
        public string RequestId { get; set; }

        [DataMember(Name = "FullName")]
        public string FullName  { get; set; }

        [DataMember(Name = "OrderData")]
        public OrderData[]  OrderData { get; set; }
    }
[DataContract]
    public class OrderData
    {
        [DataMember(Name = "OrderId")]
        public string OrderId { get; set; }

        [DataMember(Name = "SourceId")]
        public string SourceId { get; set; }

        [DataMember(Name = "SourceData")]
        public SourceData[] SourceData { get; set; }
    }

    [DataContract]
    public class SourceData
    {
        [DataMember(Name = "SourceDescription")]
        public string SourceDescription { get; set; }

        [DataMember(Name = "ProductName")]
        public string ProductName { get; set; }
    }
}

This is the Deserializer I use

private static T Deserialize<T>(string jsonString)
    {
        using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonString)))
        {
            var serializer = new DataContractJsonSerializer(typeof(T));
            return (T)serializer.ReadObject(ms);
        }
    }

When I deserialize the bove jsonstring ,I am getting error message

 There was an error deserializing the object of type RecordInfo. End element 'Warning' from namespace '' expected. Found element 'item' from namespace ''.

Any suggestions to resolve this error?

Seminar answered 5/4, 2013 at 19:37 Comment(2)
which DataMember(s) do you want to be NullableAetna
@Wiemon What is "Warning" suppose to map to?Margretmargreta
K
6

Set IsRequired = false, e.g.:

[DataMember(Name = "RequestId", IsRequired = false)]

MSDN Source: DataMemberAttribute.IsRequired Property

Gets or sets a value that instructs the serialization engine that the member must be present when reading or deserializing.

Kittrell answered 5/4, 2013 at 19:39 Comment(5)
For my code I am getting an error message during deserialization There was an error deserializing the object of type RecordInfo. End element 'Warning' from namespace '' expected. Found element 'item' from namespace ''.Seminar
Your problem is somewhere else: you've declared Warning property as string, but your JSON contains an object: ""Warning"":[].Kittrell
in case there is a "Warning":["WARNING_no data ForCustomer"] how my warning object should look like if I create a datacontract like [DataContract(Name = "Warning")] public class Warning { [DataMember(Name = "Message", IsRequired = false)] public string Message { get; set; } } ,it wont return the messageSeminar
Change your Warning property definition to be string[]: public string[] Warning { get; set; }Kittrell
For my code i am getting following error {"There was an error deserializing the object of type System.String. End element 'root' from namespace '' expected. Found element 'menu' from namespace ''."} What should i do to resolve itMisbehave
A
2

Another reason I found for the similar error is when we map an array type of Json field to a non array field of the data contract class. (e.g.) my JSON data was like -

"ipAddress": [
    "10.61.255.25",
    "fe80:0000:0000:0000:10e1:0b66:96a6:9ac8"
]

But because I was unaware of this type of IPAddress data, I was mapping ipaddress to

[DataMember(Name="ipAddress")]
public string IPAddress ( get; set; }

Instead, it should be

[DataMember(Name="ipAddress")]
public string[] IPAddress ( get; set; }
Aircraftman answered 2/7, 2016 at 4:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.