0

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?

Tim Boyden
  • 11
  • 3
  • I imagine you know what values to expect from the endpoints you call? I mean, you request one entity you get a specific set of values, right? – Camilo Terevinto Nov 15 '20 at 17:18
  • The property list is dynamic depending what is asked for. Customer entity has up to 225 properties that could be returned, depending on what was asked for in the request. Item entity has up to 275 properties at last count. – Tim Boyden Nov 15 '20 at 17:42
  • I should add, I don't control the json structure, I am using a software vendors' API and this is what they return. Trying to avoid having to have a separate class for each entity object returned. – Tim Boyden Nov 15 '20 at 17:44
  • Well, think about it. You cannot have static models for dynamic data. If you don't want to create classes, the only other way is to use dictionaries – Camilo Terevinto Nov 15 '20 at 18:35
  • @Kalten I will mark that as the answer. I was hoping because I could access the data with a Dictionary object and subsequently with a KeyValuePair that that could mean a class or two could be made using properties representing the Dictionary and KeyValuePair objects and deserialize into a C# object I could use. But sounds like I will have to use the loop to build a list of the objects I want, populated from the values extracted from the loop. I don't want to have to build concrete classes for each entity, because they can change through extension and application updates. – Tim Boyden Nov 16 '20 at 18:23

0 Answers0