I have to deserialize a JSON string to C# classes inorder to bind to a grid. I have implemented the respective classes in C#. But at a particular instance, this fails because the JSON property will be either an array or an object. Please check a part of the string.
I have created ItemList class with 3 properties IL1 , Name and another object class "Item". However, you can see that when the property "Name" is Rubber, I should have List of Item class as a property rather than Item object. When it is Rubber, it returns array of 2 items. How can I parse this to a C# class effectively and the easiest way to get this done?
"ItemList": [
{
"@IL1": "Yes",
"@Name": "Pencil"
"Item": {
"@ItemType": "Pencil",
"@Item2": "1A7716744F7048ACA2549BE93F0A2BF1",
"aimLink": {
"@id": "1A7716744F7048ACA2549BE93F0A2BF1",
"@DisplayText": "P00001"
}
}
},
{
"@IL1": "Yes",
"@Name": "Pen",
"Item": {
"@ItemType": "Pen",
"@Item2": "AE067F7EDB6147C09AED243C1F7FAD25",
"aimLink": {
"@id": "AE067F7EDB6147C09AED243C1F7FAD25",
"@DisplayText": "5100010654120001
}
}
},
{
"@IL1": "Yes",
"@Name": "Rubber",
"Item": [
{
"@ItemType": "Rubber",
"@ItemGID": "622025629037499394DF092DA16BAB7F",
"aimLink": {
"@id": "622025629037499394DF092DA16BAB7F",
"@DisplayText": "12345678-1234-123456-7116#01"
}
},
{
"@ItemType": "Rubber",
"@ItemGID": "F336F65F8E014E80B84A2312F829493C"
"aimLink": {
"@id": "F336F65F8E014E80B84A2312F829493C",
"@DisplayText": "12345678-1234-123456-7116#14"
}
}
]
}
],
What I have tried:
I tried to adopt Generic approach, but couldn't proceed much.
Thanks,
Divya