© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
10 replies
illy

❔ Deserializing JSON

I'm working on a project where I need to deserialize a JSON api response, and do things with the data. This is a little demo thing:
class Response {
    [JsonProperty("success")]
    [DefaultValue(false)]
    public bool Success { get; private set; }

    [JsonProperty("days")]
    public Days Days { get; private set; }
}

class Days {
    [JsonProperty("first_day")]
    [DefaultValue("monday")]
    public string FirstDay { get; private set; }
}
class Response {
    [JsonProperty("success")]
    [DefaultValue(false)]
    public bool Success { get; private set; }

    [JsonProperty("days")]
    public Days Days { get; private set; }
}

class Days {
    [JsonProperty("first_day")]
    [DefaultValue("monday")]
    public string FirstDay { get; private set; }
}


My issue is, I need to be able to use
Response.Days.FirstDay
Response.Days.FirstDay
, when my JSON response looks like this:
{'success': true, 'first_night': 'friday'}
{'success': true, 'first_night': 'friday'}

Essentially, I want the
DefaultValue
DefaultValue
to be present, even if the JSON doesn't contain the
'first_day'
'first_day'
key.
I'm really not sure how to do this, because I'd need to do this with dozens of attributes in about 6 subclasses, so doing it all manually would take ages and doesn't seem good for maintainability.

Any help on how I can achieve this result? To get everything to 'exist' even if it doesn't exist in the JSON, just by using the default value?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ Problems with deserializing json
C#CC# / help
3y ago
❔ Deserializing Json to class
C#CC# / help
3y ago