0

I am reading 2 REST APIs using Httpclient in C#. The APIs return following employee data in JSON format:

1st API

{
    "status": "OK", 
    "content": {
        "empid1": 89900,
        "empid2": 45550,
        "empid3": 22350}
}

2nd API

 {
    "status": "OK", 
     "content": {
              "empid1": "grade1",
              "empid1": "grade2",
              "empid1": "grade2"}}

Classes defined and code used is as follows:

 public class content
    {
        public string empid { get; set; } // e.g. empid3
        public double salary { get; set; } // e.g. 89900
        public string grade { get; set; } // e.g. Grade1
    }
    public sealed class WrapperEmployees
    {
        [JsonProperty("status")]
        public string Status { get; set; }

        [JsonProperty("data")]
        public List<content> empdata { get; set; } = new List<data>();
    }

To deserialize, used this-

WrapperEmployees nj = JsonConvert.DeserializeObject<WrapperEmployees>(response);

But, last line gives error on deserialization:

Cannot deserialize current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[CsharpSample.App_Code.Employee]' 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 'content.emp1', line 4, position 18.

Is my class structure incorrect? My ultimate aim is to fetch common data from both APIs against employees.

James Z
  • 12,209
  • 10
  • 24
  • 44
TkTech
  • 37
  • 7
  • 1
    you have duplicate keys `"empid1"`. although JSON _technically_ does not forbid this, you definitely should _not_ do stuff like this unless you _absolutely_ have to. talk to whoever's responsible for this API and make them fix their code to be less awful. – Franz Gleichmann Jul 05 '21 at 11:47
  • Unrelated : `public double salary { get; set; } // e.g. 89900` - do not use floating point for monetary amounts. – Fildor Jul 05 '21 at 11:47
  • And the json property is named "content", not "data". – Fildor Jul 05 '21 at 11:48
  • 1
    `into type 'System.Collections.Generic.List\`1[CsharpSample.App_Code.Employee]'` hints to that you are not posting the code that creates the exception. – Fildor Jul 05 '21 at 11:50
  • Does this answer your question? [How to parse unreadable json?](https://stackoverflow.com/questions/68253204/how-to-parse-unreadable-json) – Amit Verma Jul 05 '21 at 11:51
  • Please post an [mcve]. – Fildor Jul 05 '21 at 11:52
  • @Fildor , Have corrected content to data, issue persists – TkTech Jul 05 '21 at 12:03
  • 1
    Try `public Dictionary empdata { get; set; } = new Dictionary();` then map it into the object yourself, based on whether the value is a `string` or an `int` – Charlieface Jul 05 '21 at 12:03
  • @Charlieface , Thanks, your solution worked. Please put it as answer, so I can Mark it 'Accepted' – TkTech Jul 08 '21 at 07:31
  • Does this answer your question? [json deserialization to C#](https://stackoverflow.com/questions/65727513/json-deserialization-to-c-sharp) – Charlieface Jul 08 '21 at 12:35
  • Yes, @Charlieface . – TkTech Jul 10 '21 at 06:21
  • @Fildor Please vote for a suitable dupe – Charlieface Jul 10 '21 at 22:54

1 Answers1

0

Option 1: use specific classes for each json deserialization:

class EmployeesSalaries {
  public string Status { get; set; }
  public Dictionary<string, int> content { get; set; };
}
class EmployeesGrades {
  public string Status { get; set; }
  public Dictionary<string, string> content { get; set; };
}

Option 2: deserialize to common class, but you will get 'good' content values only if they are int/string. If they will be objects - you will have JObjects as values.

class EmployeesData {
  public string Status { get; set; }
  public Dictionary<string, object> content { get; set; };
}
GraphWalk
  • 319
  • 3
  • 9