请帮助它打破我的大脑。
无法将当前的'System.Collections.Generic.List`1ExaAsset+UsersInBinList‘对象(例如,{“名称”:“值”})反序列化为
类型,因为该类型需要一个JSON数组(例如,1,2,3)才能正确地反序列化。要修复此错误,要么将JSON更改为JSON数组(例如,1,2,3),要么更改反序列化类型,使之成为可以从JSON对象反序列化的普通.NET类型(例如,不像整数这样的原始类型,而不是数组或列表之类的集合类型)。还可以将JsonObjectAttribute添加到类型中,以强制它从JSON对象反序列化。路径'topUsers.usersInBinList'dwm-2 (xxxx-xxx-10)‘..username’,第1行,位置855‘
"usersInBinList": {
"dwm-2": {
"username": "dwm-2",
"fullName": "Service Account",
"riskScore": 4.82
}}
public class UsersInBinList
{
public string username { get; set; }
public string fullName { get; set; }
public string photo { get; set; }
public string title { get; set; }
public double riskScore { get; set; }
}
public class TopUsers
{
public string key { get; set; }
public string modelName { get; set; }
public string groupingFeatureValue { get; set; }
public string histSpan { get; set; }
public string histogramDate { get; set; }
public string histClassName { get; set; }
public double confidenceFactor { get; set; }
public int totalBinCount { get; set; }
public int totalCount { get; set; }
public long lastUpdate { get; set; }
public Hist hist { get; set; }
public Dictionary<string, List<UsersInBinList>> UsersInBinList { get; set; }
public PeerGroupsInBinList peerGroupsInBinList { get; set; }
public bool disabled { get; set; }
}
asset = JsonConvert.DeserializeObject<ExaAsset>(APIresponse);UsersInBinList的第一个键是用户名,而不是“用户”等等,所以我无法将它序列化为每个API响应的不同用户名.有什么想法吗?
发布于 2021-07-21 19:15:42
似乎您需要将属性UsersInBinList更改为Dictionary of string和UsersInBinList,而不是UsersInBinList列表。
public Dictionary<string, UsersInBinList> UsersInBinList { get; set; }发布于 2021-07-21 19:17:49
我有两点意见要告诉你:
如果JSON中的属性名称不能很好地映射到您在https://www.newtonsoft.com/json/help/html/JsonPropertyName.htm中指定的名称,则可以使用JsonPropertyAttribute将其表示为Newtonsoft库
https://stackoverflow.com/questions/68475233
复制相似问题