Reputation: 1
How can i perform custom deserialization of this object in .Net Core C#, that Afpak is assigned as 'string name' in this object class. I have the following JSon Code of the object:
{
"Afpak": {
"id": 1,
"race": "hybrid",
"flavors": [
"Earthy",
"Chemical",
"Pine"
],
"effects": {
"positive": [
"Relaxed",
"Hungry",
"Happy",
"Sleepy"
],
"negative": [
"Dizzy"
],
"medical": [
"Depression",
"Insomnia",
"Pain",
"Stress",
"Lack of Appetite"
]
}
}
EDIT: Note the object class is Strain, it gets variable name from root of this json - in this case the name variable would be Afpak, there is 100s of different Strain class objects in this one JSON code is what I am having trouble with as it doesnt make sense to create each one as seperate class if it is all of the same class.
Upvotes: 0
Views: 47
Reputation: 1165
you should implement a class exposing the properties you want to map from that JSON object. Something like this:
public class AfpakDto{
public string id {get;set;}
public string race {get;set;}
public string[] flavors {get;set;}
}
public class FooDto {
public AfpakDto Afpak {get; set;}
}
and then use whatever library you want. Here's a nice article about System.Text.JSON: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0
Upvotes: 1