Error deserializing an object of type .... The final element '......' from namespace '' is expected. Found item 'item' from namespace ''

When I deserialize my jsonstring, I get an error

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"":"" abc@abc.com "", ""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 that 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 bove jsonstring, I get an error

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

Any suggestions for fixing this error?

+4
source share
2 answers

Set IsRequired = false , for example:

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

MSDN Source : DataMemberAttribute.IsRequired Property

Gets or sets a value that indicates to the serialization engine that a member should be present when reading or deserializing.

+5
source

Another reason why I found a similar error is to compare the array type of the Json field with the field without an array of the data class. (e.g. my JSON data looked like

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

But because I did not know this IPAddress data type, I matched IPAddress with

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

Instead, he should be

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

All Articles