Mac
Mac

Reputation: 53

Nested Json into Object Class with Variable Key names

Please help its breaking my brain.

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[ExaAsset+UsersInBinList]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'topUsers.usersInBinList['dwm-2 (xxxx-xxx-10)'].username', line 1, position 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);

The first key of UsersInBinList is a username instead of "user" etc. so I am unable to serialise it as its a different username for each API response... Any ideas?

Upvotes: 0

Views: 639

Answers (2)

John Townsend
John Townsend

Reputation: 336

I have 2 observations for you:

  1. I believe the JSON package is giving you issues because you are trying to deserialize a JSON object into a JSON array.

  2. If the names of properties in your JSON don't map well to the names you have specified in C#, then you can use the JsonPropertyAttribute to signify this to the Newtonsoft JSON library https://www.newtonsoft.com/json/help/html/JsonPropertyName.htm

Upvotes: 1

Guru Stron
Guru Stron

Reputation: 141720

It seems that you need to change your property UsersInBinList to Dictionary of string and UsersInBinList, not list of UsersInBinList:

 public Dictionary<string, UsersInBinList> UsersInBinList { get; set; }

Upvotes: 1

Related Questions