Consuming json API in C# application – part 2

By | 1. March 2023

In previous post Consuming json API in C# application we created classes vor deserializing web api json data. By default created class properties are with same name as json elements.

To deserialize JSON using different property names in C#, you have a few options:

– You can use the JsonPropertyName attribute to specify the JSON property name for each class property¹³.
– You can use a naming policy (built-in or custom) to convert between JSON property names and class property names²³.
– You can use a Dictionary<string, T> to deserialize JSON with variable property names¹.

Here is an example of using the JsonPropertyName attribute:

 

—————————————————————

(1) c# – Deserialize JSON with variable property names – Stack Overflow.
(2) C# – Deserialize JSON using different property names.
(3) How to serialize and deserialize JSON using C# – .NET.

 

Leave a Reply