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?

1

2 Answers 2

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; }
Sign up to request clarification or add additional context in comments.

4 Comments

You sure are a legend. that is exactly what I had done wrong. Its working a treat now thank you.
@Mac was glad to help!
asset.topUsers.UsersInBinList[0].fullName <-- how do I reference that... its saying 0 needs to be a string not a int?
@Mac you can use Values property of Dictionary to get all values.
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

1 Comment

yes, I was trying to redact stuff poorly. I will make it clearer next time thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.