© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
19 replies
ascpixi

❔ ✅ Deserializing a JSON array that contains a derived type AND a base type with JSON.NET?

I have three classes:
ScratchTarget
    ├─ ScratchScene
    └─ ScratchSprite
ScratchTarget
    ├─ ScratchScene
    └─ ScratchSprite

ScratchTarget
ScratchTarget
contains all common fields that
ScratchScene
ScratchScene
and
ScratchSprite
ScratchSprite
will have; I know that all of the JSON array entries will have fields from that class.

However, some objects will be of type
ScratchScene
ScratchScene
, and some will be of type
ScratchSprite
ScratchSprite
. I have a
isStage
isStage
field in
ScratchTarget
ScratchTarget
that informs me of the type of said
ScratchTarget
ScratchTarget
.

How would I go about deserializing such a JSON array to a
List<ScratchTarget>
List<ScratchTarget>
, with the JSON.NET deserializer also converting the extra fields by picking either
ScratchScene
ScratchScene
or
ScratchSprite
ScratchSprite
?
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

✅ Deserializing nested array json file.
C#CC# / help
2y ago
❔ Deserializing JSON
C#CC# / help
3y ago
✅ Problems with deserializing json
C#CC# / help
3y ago