When I deserialize my jsonstring ,I am getting error message
当我反序列化我的jsonstring时,我收到错误消息
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
这是我的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 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
当我反序列化bove jsonstring时,我收到错误消息
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?
有什么建议来解决这个错误吗?
2 个解决方案
#1
5
Set IsRequired = false
, e.g.:
设置IsRequired = false,例如:
[DataMember(Name = "RequestId", IsRequired = false)]
MSDN Source: DataMemberAttribute.IsRequired
Property
MSDN来源:DataMemberAttribute.IsRequired属性
Gets or sets a value that instructs the serialization engine that the member must be present when reading or deserializing.
获取或设置一个值,该值指示序列化引擎在读取或反序列化时必须存在该成员。
#2
1
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 -
我发现类似错误的另一个原因是我们将数组类型的Json字段映射到数据协定类的非数组字段。 (例如)我的JSON数据就像 -
"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
但是因为我没有意识到这种类型的IPAddress数据,所以我将ipaddress映射到
[DataMember(Name="ipAddress")]
public string IPAddress ( get; set; }
Instead, it should be
相反,它应该是
[DataMember(Name="ipAddress")]
public string[] IPAddress ( get; set; }
#1
5
Set IsRequired = false
, e.g.:
设置IsRequired = false,例如:
[DataMember(Name = "RequestId", IsRequired = false)]
MSDN Source: DataMemberAttribute.IsRequired
Property
MSDN来源:DataMemberAttribute.IsRequired属性
Gets or sets a value that instructs the serialization engine that the member must be present when reading or deserializing.
获取或设置一个值,该值指示序列化引擎在读取或反序列化时必须存在该成员。
#2
1
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 -
我发现类似错误的另一个原因是我们将数组类型的Json字段映射到数据协定类的非数组字段。 (例如)我的JSON数据就像 -
"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
但是因为我没有意识到这种类型的IPAddress数据,所以我将ipaddress映射到
[DataMember(Name="ipAddress")]
public string IPAddress ( get; set; }
Instead, it should be
相反,它应该是
[DataMember(Name="ipAddress")]
public string[] IPAddress ( get; set; }