1

I have 2 JSON objects that are deserialized into a c# object

the JSON objects both look like this

{
"obj2": {
  "booleanVal": true,
  "data": "me",
  "foo": {
    "Key": "",
    "booleanVal": false,
    "dataValue": "foo"
  },
  "Ide": {
    "booleanVal": true,
    "foo2": {
        "booleanVal": false,
        "dataValue": "foo"
      }
   }
}
{
"obj1": {
  "booleanVal": true,
  "data": "me",
  "foo": {
    "Key": "",
    "booleanVal": false,
    "dataValue": "foo"
  },
  "Ide": {
    "booleanVal": true,
    "foo2": {
        "booleanVal": false,
        "dataValue": "foo"
      }
   }
}

I want to loop through the c# object obj1 check the booleanVal to compare it to the booleanval of obj2, I cant seem to figure out a good way to go through the object to the last nested object the reach the booleanVal, what i tried so far is something like this

private void loop(obj1, obj2)
{
  foreach (var p in obj1)
            {
                foreach (var c in obj2)
                {

                    if (p.booleanVal != c.booleanVal)
                    {
                        Console.Error.WriteLine("illegal");
                    }
                }
            }
}
Asker
  • 97
  • 1
  • 11
  • are you iterating through two lists? – Kevin Mar 05 '20 at 11:30
  • Add more details about what you exactly need to achieve. – as-if-i-code Mar 05 '20 at 11:38
  • 1
    Does this answer your question? [Working with C#: How do I iterate through a nested JSON using Newtonsoft.JSON?](https://stackoverflow.com/questions/45418156/working-with-c-how-do-i-iterate-through-a-nested-json-using-newtonsoft-json) – solid.py Mar 05 '20 at 13:14

1 Answers1

0

You can use linq.
Add below line at top of your .cs file.

using System.Linq;

There are many possible ways to easily iterate through list using linq. Below 2 examples will give you an idea about how to reach till last nested object and compare it. Maybe you need to modify it according to your need.

Example 1: Match booleanVal separately at every level

foreach (var obj1Item in obj1)
{
    if(obj2.Any(obj2Item => obj2Item.booleanVal == obj1Item.booleanVal))
    {
        // first level booleanVal matched
    }

    if(obj2.Any(obj2Item => obj2Item.foo.booleanVal == obj1Item.foo.booleanVal))
    {
        // second level booleanVal matched
    }

    if(obj2.Any(obj2Item => obj2Item.Ide.booleanVal == obj1Item.Ide.booleanVal))
    {
        // second level booleanVal matched
    }

    if(obj2.Any(obj2Item => obj2Item.Ide.foo2.booleanVal == obj1Item.Ide.foo2.booleanVal))
    {
        // third level booleanVal matched
    }

}

Example 2: Match booleanVal at all levels at the same time

foreach (var obj1Item in obj1)
{
    if(obj2.Any(obj2Item => obj2Item.booleanVal == obj1Item.booleanVal && 
                            obj2Item.foo.booleanVal == obj1Item.foo.booleanVal &&
                            obj2Item.Ide.booleanVal == obj1Item.Ide.booleanVal &&
                            obj2Item.Ide.foo2.booleanVal == obj1Item.Ide.foo2.booleanVal))
    {
        // booleanVal at all levels matched
    }
}

as-if-i-code
  • 2,103
  • 1
  • 10
  • 19