If you have a non-trivial JSON object, it can be time consuming to create classes the can be used for serialization/deserialization. This web site can help you out no matter what type of language you want to work with.
Procedure
- Start by bringing up the web site: https://app.quicktype.io/
- On left side of the screen:
- Paste a full example of your JSON file in the left panel.
- Edit the class name if desired.
- Set the
Source
type to “JSON”.
- On the right side of the screen:
- Select the output code language.
- Select the serialization framework:
- Newton.Soft – add NuGet ‘Newtonsoft.Json’
- System.Text.Json – add NuGet ‘System.Text.Json’
- Edit namespace if desired.
- Select the format for JSON arrays if needed:
- Array (C# example is “Account[]”)
- List (C# example is “List<Account>”)
- Select the “Other” tab is desired to further customize the classes.
The resulting classes are regenerated automatically if you modify any settings or change the JSON file.
Copy the class to your code. The source code even includes a helpful example of how to deserialize a string to your class.