Click here to Skip to main content
15,860,972 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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"}
}


What I have tried:

Classes defined and code used is as follows:
C#
public class content
{
    public string empid { get; set; } // e.g. empid3
    public float 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("content")]
    public List<content> empdata { get; set; } = new List<content>();
}


To deserialize, used this-
C#
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<T>) 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.


Can you help me resolving this ? My ultimate aim is to fetch common data from both APIs against employee IDs.
Posted
Updated 31-Jul-22 16:52pm
v4

Quote:
C#
[JsonProperty("data")]
public List<data> empdata { get; set; } = new List<data>();
According to the samples in your question, neither API returns a property called data.

They both have a property called content, but it is not a list; it's an object.

Assuming the property names aren't fixed, you could deserialize the first response into a Dictionary<string, int>:
C#
class FirstApiResponse
{
    [JsonProperty("status")]
    public string Status { get; set; }
    
    [JsonProperty("content")]
    public Dictionary<string, int> Content { get; set; }
}

The second API response cannot be deserialized into anything, since it uses the same property name for multiple properties in a single object.
 
Share this answer
 
Comments
Member 15244870 5-Jul-21 8:28am    
Thanks Richard for quick reply, I have corrected data to content, but issue exists. Kindly check now
Richard Deeming 5-Jul-21 8:31am    
So you fixed one of the two issues I mentioned in my solution, and your code still doesn't work?

If only there was some clue as to what you should do next...
Member 15244870 5-Jul-21 8:37am    
Richard, thanks Again. Your solution worked. But without serializing 2nd API, how will I extract common data of both API. So I have taken another class SecApiResponse as foll:
public Dictionary<string, string> empdata { get; set; }
public string Status { get; set; }

Then I'll execute a Linq query against both classes to get common results. Let me know if it is correct
Richard Deeming 5-Jul-21 8:46am    
The sample of your second API response in your question shows an object with three properties, all named empid1.

If that's really what the response looks like, then it's not valid JSON. You won't be able to deserialize it to anything without a custom JSON converter.

If the object actually has different property names, as in the sample response from the first API, then you can deserialize it to a Dictionary<string, string>, and then use LINQ (or a foreach loop) to combine the responses.
Member 15244870 5-Jul-21 9:07am    
Ok, I have taken one single class, dictionary argument is object, see as follows:
public sealed class APIResponse
{
[JsonProperty("status")]
public string Status { get; set; }

[JsonProperty("data")]
public Dictionary<string, object=""> empdata { get; set; }
}

Now I shall read response of 2 APIS into 2 diff objects of above class. Then I shall do a foreach loop inside both objects to get common data based on empid. Hope it sounds ok ?
Solution: 1
Make sure your both API returns as below
[{
        "status": "OK", 
        "content": {
            "empid1": 89900,
            "empid2": 45550,
            "empid3": 22350}
    }]{
Solution : 2
Update your wrapper class property as below
 [JsonProperty("content")]
 public content empdata { get; set; }

Try only either any one of these solutions definitely it works
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900