user2086641
user2086641

Reputation: 4371

Deserialize response from API using custom class c#

I am receiving the json response of below format from an api. I am trying to deserialze it with custom class.

{
    "TraceEvent": {
        "Attributes": {
            "Commodity": "APPLES",
            "Variety": "Green"
        },
        "Codes": [{
                "devicename": "",
                "code": "901491877572115",
                "timestamp": "2018-02-15T19:33:29.4418926+05:30"
            }, {
                "devicename": "",
                "code": "6657287134488755",
                "timestamp": "2018-02-15T19:33:29.4418926+05:30"
            }
        ]
    }
}

Below is my custom class used for deserialize

public class EventContainer
{
    [JsonProperty("TraceEvent")]
    public TraceEvent TraceEvent { get; set; }
} 

public class TraceEvent
{
    [JsonProperty("attributes")]
    public TraceAttributes Attributes { get; set; }
    [JsonProperty("codes")]
    public TraceCodes Codes { get; set; }
}

public class TraceAttributes
{
    [JsonProperty("commodity")]
    public string Commodity { get; set; }
    [JsonProperty("variety")]
    public string Variety { get; set; }
}

public class TraceCodes
{

    public TraceCodes()
    {
        Codes = new List<TraceCode>();
    }
    [JsonProperty("Codes")]
    public List<TraceCode> Codes { get; set; }
}

public class TraceCode
{
    [JsonProperty("devicename")]
    public string DeviceName { get; set; }
    [JsonProperty("code")]
    public string Code { get; set; }
    [JsonProperty("timestamp")]
    public DateTime Timestamp { get; set; }
}

In the receiver side, i am getting null for the Codes. Plesae refer my debug screen in api receiver code,

enter image description here

Can any one tell me how to rewrite my custom class to deserialize the Codes list from JSON api

Thanks for the help.

Upvotes: 1

Views: 99

Answers (2)

poke
poke

Reputation: 388273

TraceEvent has a property

public TraceCodes Codes { get; set; }

And TraceCodes is another object with a list of codes:

public List<TraceCode> Codes { get; set; }

This would mean there would have to be a structure like this:

{
    "TraceEvent": {
        "Codes": {
            "Codes": [
                { … },
                { … },
            }
        }
    }
}

So the "Codes" part is double. Instead, you need to modify your TraceEvent to have that list directly:

public class TraceEvent
{
    [JsonProperty("attributes")]
    public TraceAttributes Attributes { get; set; }

    [JsonProperty("Codes")]
    public List<TraceCode> Codes { get; set; }
}

Btw. that should have actually resulted in a JsonSerializationException, so you should check whether that gets swallowed somewhere.

Upvotes: 2

Rudresha Parameshappa
Rudresha Parameshappa

Reputation: 3926

Change the class structure. The Codes should be in TraceEvent class not in its own class

public class TraceEvent
{
    [JsonProperty("attributes")]
    public TraceAttributes Attributes { get; set; }
    [JsonProperty("Codes")]
    public List<TraceCode> Codes { get; set; }
}

Remove below class

public class TraceCodes
{

    public TraceCodes()
    {
        Codes = new List<TraceCode>();
    }
    [JsonProperty("Codes")]
    public List<TraceCode> Codes { get; set; }
}

Upvotes: 4

Related Questions