I am learning C# and I am trying to parse json/xml responses and check each and every key and value pair. For xml I am converting to json so I have only one function/script to work with both cases. My issue is that I am working with a wide range of json responses which are not similar and there may be arrays in some of the json response. I have tried accessing the "Count" of the json object as a way to check for arrays.
Note: The responses will vary. This example is for Products > Product > name, quantity and category. The next response will change and can be like Country > State > Cities and so on. I cannot rely on creating classes since all responses are going to be different. Plus I am working on automating it so it should be able to handle anything thrown at it.
Sample Json I am working with:
{
  "products": {
    "product": [
      {
        "name": "Dom quixote de La Mancha",
        "quantity": "12",
        "category": "Book"
      },
      {
        "name": "Hamlet",
        "quantity": "3",
        "category": "Book"
      },
      {
        "name": "War and Peace",
        "quantity": "7",
        "category": "Book"
      },
      {
        "name": "Moby Dick",
        "quantity": "14",
        "category": "Book"
      },
      {
        "name": "Forrest Gump",
        "quantity": "16",
        "category": "DVD"
      }
    ]
  }
The way I am accessing the count, name and value is as follows:
dynamic dyn = JsonConvert.DeserializeObject<dynamic>(jsonText);
foreach (JProperty property in dyn.Properties())
{
    string propname = property.Name;
    var propvalue = property.Value;
    int count = property.Count;
}
Is there a way to access these without going through the foreach loop like int count = dyn.Count ? All I am getting from this is null instead of actual values.
For the above example my end result will be like: This responses contains products> product> 5 x (name, quantity, category)
The QuickWatch for the object: QuickWatch for dyn object
 
    