I have a JSON file that has a property called craftingrequirements. This property can be an object or an array, what is the best way to process data in such a case?
Currently I have set the type to object for the property because otherwise there would be an error.
"weapon": [
{
"@uniquename": "T3_2H_QUARTERSTAFF",
"craftingrequirements": [
{
"@silver": "0",
"@time": "5",
"@craftingfocus": "16085",
},
{
"@silver": "0",
"@time": "5",
"@craftingfocus": "16085"
}
]
},
{
"@uniquename": "T6_2H_QUARTERSTAFF_AVALON",
"craftingrequirements": {
"@silver": "0",
"@time": "1",
"@craftingfocus": "980"
}
}
]
That didn't work either..
var craftingRequirement = equipmentItem.CraftingRequirements as CraftingRequirements;
if (craftingRequirement == null)
{
var craftingRequirements = equipmentItem.CraftingRequirements as List<CraftingRequirements>;
}
if (equipmentItem.CraftingRequirements is CraftingRequirements)
{
}
else if (equipmentItem.CraftingRequirements is List<CraftingRequirements>)
{
}
I work with .net / C# and haven't found a good solution yet.
CraftingRequirementsas List in your c# classes. Then during deserialization check whether that property is an array or an object. If it's an array, you don't need to do anything. If it's an object, place that object in theCraftingRequirementsList. So in your c# code you can always be sure thatCraftingRequirementsis a List, even if it sometimes only has one element.