I need to reduce one collection of nested lists to another. I need to keep only objects which have parameter IsOk == true.
public class Item {
public string Id { get; set; }
public Item[] Items { get; set; }
public bool IsOk { get; set; }
This is enter json:
{
'id': '1',
'isOk': false,
'items': [
{
'id': '21',
'isOk': false,
'items': [
{
'id': '31',
'isOk': true,
'items': [
{
'id': '41',
'isOk': true,
'items': null
},
{
'id': '42',
'isOk': true,
'items': null
}
]
}
]
},
{
'id': '22',
'isOk': true,
'items': [
{
'id': '32',
'isOk': true,
'items': [
{
'id': '43',
'isOk': true,
'items': null
},
{
'id': '44',
'isOk': true,
'items': null
}
]
}
]
}
]
}
This is correct modified json.
[
{
'id': '31',
'isOk': true,
'items': [
{
'id': '41',
'isOk': true,
'items': null
},
{
'id': '42',
'isOk': true,
'items': null
}
]
},
{
'id': '22',
'isOk': true,
'items': [
{
'id': '32',
'isOk': true,
'items': [
{
'id': '43',
'isOk': true,
'items': null
},
{
'id': '44',
'isOk': true,
'items': null
}
]
}
]
}
]
As you see new json have modified hierarchy and keep only objects with property IsOk == true. The biggest problem for me is how to change the hierarchy of the objects. Currently I try to use recursive function but maybe better is to use while loop?