1

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.

17
  • How do you deserialize? Commented Feb 16, 2018 at 9:25
  • I feel like you'd be better of making either a custom modelbinder if you use asp.net mvc model binding, or a custom jsonconverter if you use JSON.NET to deserialize. Commented Feb 16, 2018 at 9:28
  • You provide a class, but how does it relate to EventContainer? Commented Feb 16, 2018 at 9:29
  • 1
    What? That sentence makes little sense. Please add the class definition to your question. Commented Feb 16, 2018 at 9:32
  • 1
    TraceCodes should be List Commented Feb 16, 2018 at 9:39

2 Answers 2

4

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

Comments

2

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.

3 Comments

In his screenshot he has EventContainer, but he hasn't posted the definition.
Turns out he has a typo.
Oh well~ – Thanks for the heads-up john!

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.