I have a json structure of:
{
"Items": [
{
"CustNum": " 11",
"CustSeq": "0",
"Name": "Kay's Bike Shop",
"_ItemId": "PBT=[customer] …"
},
{
"CustNum": " 12",
"CustSeq": "0",
"Name": "Lake House Cycles",
"_ItemId": "PBT=[customer] …"
}
]
}
Which in C# can be represented as:
List<Dictionary<string, object>>
Or
public partial class ItemsList
{
[JsonProperty("Items")]
public List<Item> Items { get; set; }
}
public partial class Item
{
[JsonProperty("CustNum")]
public string CustNum { get; set; }
[JsonProperty("CustSeq")]
[JsonConverter(typeof(ParseStringConverter))]
public long CustSeq { get; set; }
[JsonProperty("Name")]
public string Name { get; set; }
[JsonProperty("_ItemId")]
public string ItemId { get; set; }
}
The classes (as generated by a tool such as QuickType) will not work for me because the objects returned have a variable key/value returned depending on what entity is queried. In other words, it will not always be CustNum, CustSeq, and Name (Customer entity) returned. It could be ProductNum, Description, Revision (Item entity) returned.
Using List<Dictionary<string, object>>
I know I can access the values with a loop such as:
foreach(Dictionary<string, object> item in Items)
{
foreach(KeyValuePair<string, object> property in item)
{
Console.WriteLine($"{property.Key}: {property.Value}");
}
}
However, there must be a way to objectify this so I can access the property values in a more direct manner. But I cannot seem to come up with an object structure that will deserialize correctly using JSON.NET. Suggestions?