-1

I am beginner to C#...I have a following JSON object(which contains arrays) in C#, i want to loop through it,

{"maindepartment":[
{"id":0,"level":0,"label":"Employee Names","departments":[
    {"id":0,"level":1,"label":"IT","deparmentslevel1":[
        {"id":0,"level":2,"label":"dep1"},
        {"id":0,"level":2,"label":"dep2"}]},
    {"id":0,"level":1,"label":"dept","deparmentslevel1":[
        {"id":0,"level":2,"label":"dep3"},
        {"id":0,"level":2,"label":"dep4"}]}]},
{"id":0,"level":0,"label":"Employee Cities","cities":[
    {"id":0,"level":1,"label":""},
    {"id":0,"level":1,"label":"<p>New <strong>Jersey<\/strong><\/p>\n"},
    {"id":0,"level":1,"label":"<p>New & South - <strong>Whales<\/strong><\/p>\n"},              

{"id":0,"level":1,"label":"3223456"},
    {"id":0,"level":1,"label":"3465221"},
    {"id":0,"level":1,"label":"e"},
    {"id":0,"level":1,"label":"ee"},
    {"id":0,"level":1,"label":"ef"},
    {"id":0,"level":1,"label":"efg"},
    {"id":0,"level":1,"label":"eF!?"},
    {"id":0,"level":1,"label":"whales"},
    {"id":0,"level":1,"label":"new jersey"},
    {"id":0,"level":1,"label":"in"},
    {"id":0,"level":1,"label":"opq opq"},
    {"id":0,"level":1,"label":"ade"},
    {"id":0,"level":1,"label":"eert"},
    {"id":0,"level":1,"label":"meta"},
    {"id":0,"level":1,"label":"metadata"},
    {"id":0,"level":1,"label":"metadata from ajaja"},
    {"id":0,"level":1,"label":"testmets"},
    {"id":0,"level":1,"label":"p"},
    {"id":0,"level":1,"label":"jhonsons"},
    {"id":0,"level":1,"label":"demo"},
    {"id":0,"level":1,"label":"demo 123"},
    {"id":0,"level":1,"label":"demo Metadata 123"}]},
{"id":0,"level":0,"label"      :"Specific Terms","departments":[
    {"id":0,"level":1,"label":"demo"},
    {"id":0,"level":1,"label":"new maindepartment 1111"},
    {"id":0,"level":1,"label":"secret maindepartment"}]}]};

how to loop over it and get values of inner elements?

Learner
  • 23
  • 2
  • 8
  • What have you tried doing and what is the exact problem? Show the code and explain the issues – UnholySheep Nov 26 '17 at 15:24
  • initially it was a string.. i converted it to json object, i know how to loop over a simple json object but dont have much ideas of working with such complex ones. – Learner Nov 26 '17 at 15:26

1 Answers1

1

Firstly you have to parse this JSON to C# class (or work with C# dynamic). To do you, just copy your JSON data into the textbox in this web site and it will generate C# classes based on the JSON data format.

Take the classes it generated into your project.

Secondly you have to parse a JSON string data to C# classes. You can do it by:

YourClass data = Newtonsoft.Json.JsonConvert.DeserializeObject<YourClass>(jsonDataString);

Then you can iterate over the C# objects (in data) like you normally do.

Jacob
  • 3,598
  • 4
  • 35
  • 56
  • that worked.. but how to access inner elements now..i can access elements of maindepartmnt[] using foreach loop but not inner elements. – Learner Nov 27 '17 at 05:48
  • Why you can not access them? Are they null? – Jacob Nov 27 '17 at 05:54
  • working now thanks..silly typo mistake earlier...:-P – Learner Nov 27 '17 at 06:18
  • Trying to learn C#. About your answer: "Take the classes it generated into your project." Do I take the classes as separate files or have all the classes in same file. The JSON, I gave generated "partial classes". I am guessing they can stay in one file? Could you please clarify? – Monnie_tester May 11 '20 at 00:19
  • 1
    @Monnie_tester Several classes in single file is OK and it works, But it is bad practice. Means that for your order and clarity, you might want to separate them over multiple files. – Jacob May 11 '20 at 12:20