-1

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

Lex
  • 11
  • 2
  • You could easily use Newtonsoft.Json.Deserialize() to get an object built from your json – DudeWhoWantsToLearn Feb 09 '21 at 07:19
  • 1
    Here is how you create class representing the json. https://stackoverflow.com/questions/21611674/how-to-auto-generate-a-c-sharp-class-file-from-a-json-string. – Drag and Drop Feb 09 '21 at 07:26
  • Does this answer your question? [Deserialize JSON with C#](https://stackoverflow.com/questions/7895105/deserialize-json-with-c-sharp) – cmos Feb 09 '21 at 07:46
  • Hi all. I have edited my question to add some more context. I cannot generate and create classes since I am going to get 100s of different responses which might be used only once. – Lex Feb 09 '21 at 08:39
  • @DudeWhoWantsToLearn yes I am using `dynamic dyn = JsonConvert.DeserializeObject(jsonText);`. Changing it to `JObject dyn = JsonConvert.DeserializeObject(jsonText);` does let me access the values instead of getting null though. I am still reading and trying out different methods. Thanks. – Lex Feb 09 '21 at 08:40

1 Answers1

0

Try to deserialize your JSON into JObject like below:

var jObject = JsonConvert.DeserializeObject<JObject>(jsonText);
Rajeev Kumar
  • 371
  • 2
  • 9
  • 1
    Yes this works. I also tried `JObject dyn = JsonConvert.DeserializeObject(jsonText);` which works too. Thanks – Lex Feb 09 '21 at 09:05