I'm running into some issues trying to deserialize some JSON from an API I'm working with. For some reason the API likes to wrap the data in extra layers and arrays when it isn't necessary.
{
"CustomData": [
{
"Wrapper": [
{
"OptionalDataSet1": [
{
"ItemA": "Basic string"
},
{
"ItemB": "Another basic string"
}
]
}
]
}
]
}
Using json2csharp I've gotten classes that work for the above example.
public class OptionalDataSet1
{
public string ItemA { get; set; }
public string ItemB { get; set; }
}
public class Wrapper
{
public List<OptionalDataSet1> OptionalDataSet1 { get; set; }
}
public class CustomData
{
public List<Wrapper> Wrapper { get; set; }
}
public class RootObject
{
public List<CustomData> CustomData { get; set; }
}
The issue I'm having is the "Wrapper" class is unneeded according to the API. It's always there but will be the only item in Custom Data. Furthermore, there will only ever be a single instance of "OptionalDataSet1" inside of the Wrapper. There are other "OptionalDataSets", but again, they will be unique per request.
Finally, the Wrapper deserializes TWO objects for "OptionalDataSet1", the first with ItemA's value, the second with ItemBs. There are other data sets that can have upwards of forty items available to them, I don't want to scan through forty instances of an object to find which one has the single data attribute I'm trying to find.
Should I massage the JSON string I'm receiving from the API before sending it off to be deserialized by removing the "Wrapper" and converting the List<> properties to singular instances, or is there another method I'm missing using JSON.Net to produce something like
RootObject.CustomData.OptionalDataSet1.ItemB
Instead of
RootObject.CustomData[0].Wrapper[0].OptionalDataSet[1].ItemB