gpt4 book ai didi

c# - 错误反序列化类型的对象......预期来自命名空间 '......' 的结束元素 ''。从命名空间 'item' 找到元素 ''

转载 作者:太空狗 更新时间:2023-10-29 23:00:20 25 4
gpt4 key购买 nike

当我反序列化我的 jsonstring 时,我收到错误消息

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

这是我的 JsonString

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

数据契约

[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; }
}
}

这是我用的解串器

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);
}
}

当我反序列化上面的 jsonstring 时,我收到错误消息

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

有什么解决这个错误的建议吗?

最佳答案

设置IsRequired = false,例如:

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

MSDN 来源:DataMemberAttribute.IsRequired Property

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

关于c# - 错误反序列化类型的对象......预期来自命名空间 '......' 的结束元素 ''。从命名空间 'item' 找到元素 '',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15842136/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com