0

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?

Mac
  • 53
  • 6
  • Does this answer your question? [Hashes Are Not Matching](https://stackoverflow.com/questions/65783707/hashes-are-not-matching) – Charlieface Jul 21 '21 at 20:52

2 Answers2

1

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; }
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • You sure are a legend. that is exactly what I had done wrong. Its working a treat now thank you. – Mac Jul 21 '21 at 19:25
  • @Mac was glad to help! – Guru Stron Jul 21 '21 at 19:25
  • asset.topUsers.UsersInBinList[0].fullName <-- how do I reference that... its saying 0 needs to be a string not a int? – Mac Jul 21 '21 at 19:28
  • @Mac you can use [`Values` property](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2.values?view=net-5.0) of `Dictionary` to get all values. – Guru Stron Jul 21 '21 at 19:30
1

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

John Townsend
  • 316
  • 3
  • 6
  • yes, I was trying to redact stuff poorly. I will make it clearer next time thank you. – Mac Jul 21 '21 at 19:24