C#报错Newtonsoft.Json.JsonSerializationException: “A member with the name ‘phone‘ already
1.在做项目的时候,遇到报错信息:
Newtonsoft.Json.JsonSerializationException:
“A member with the name ‘phone’ already exists on ‘xxx.xxx.xxx.Data’. Use the JsonPropertyAttribute to specify another name.”
根据报错信息排查代码,发现实体类中
[JsonProperty(PropertyName = "phone")] public string AdminPhone { get { return adminPhone; } set { adminPhone= value; OnPropertyChanged("AdminPhone"); } }... [JsonProperty(PropertyName = "phone")] public string UserPhone { get { return userphone; } set { userphone = value; OnPropertyChanged("Phone"); } }
注解使用了两次一样的[JsonProperty(PropertyName = "phone")] 导致将对象数据转换成json格式的时候出现无法匹配的情况 需要手动删除或者修改其中一个PropertyName 即可
2.有时候发现传回后端会发现json出现了重复字段,是因为C#实体类中,字段应该使用private,而属性则使用public,如下
private string progress;//字段:使用private [JsonProperty(PropertyName = "progress")] public string Progress//属性:使用public与JsonProperty序列化 { get { return progress; } set { progress = value; OnPropertyChanged("Progress"); } }
阅读世界,共赴山海 423全民读书节,邀你共读