首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将Json嵌套到具有可变键名的对象类中

将Json嵌套到具有可变键名的对象类中
EN

Stack Overflow用户
提问于 2021-07-21 19:07:53
回答 2查看 350关注 0票数 0

请帮助它打破我的大脑。

无法将当前的'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‘

代码语言:javascript
复制
"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响应的不同用户名.有什么想法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-07-21 19:15:42

似乎您需要将属性UsersInBinList更改为Dictionary of stringUsersInBinList,而不是UsersInBinList列表。

代码语言:javascript
复制
 public Dictionary<string, UsersInBinList> UsersInBinList { get; set; }
票数 1
EN

Stack Overflow用户

发布于 2021-07-21 19:17:49

我有两点意见要告诉你:

  1. 我相信JSON包会给您带来问题,因为您试图将JSON对象反序列化为JSON数组。

如果JSON中的属性名称不能很好地映射到您在https://www.newtonsoft.com/json/help/html/JsonPropertyName.htm中指定的名称,则可以使用JsonPropertyAttribute将其表示为Newtonsoft库

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68475233

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档