1

From my previous forum question on the link, Iterate over a json input and create the treeview like hierarchy based on the "keys" taking account of the children "keys". I am building upon this question and want to create a Treeview from the content of the List<KeyId>.

The deeply nested JSON is mapped into a c# tree where each node has two properties -- a string key and a long T_id -- as well as a collection of children of the same type.

public partial class KeyIdObject
{
    public string key { get; set; }
    public long T_id { get; set; }
    public List<KeyIdObject> Children { get; set; }
}

The json has a structure as follows:

{
   "Soccer":{
      "T_id":0,
      "T_state":"valid",
      "Clubs":{
         "ClubA":{
            "T_id":"1",
            "T_state":"Champs"
         },
         "ClubB":{
            "T_id":"2",
            "T_state":"Runnerups"
         }
      },
      "Subs":{
         "SubA":{
            "T_id":"3",
            "T_state":"Unfit",
            //this is nested key
            "SubE":{
               "T_id":"3",
               "T_state":"Unfit"
            }
         }
      },
      "Subs_Used":{
         "SubK":{
            "T_id":"3",
            "T_state":"Unfit"
         }
      }
      //many more nested n-levels   
   }
}

Then it can be used to generate a recursive List like so:

public partial class KeyIdObject
{
    public static List<KeyIdObject> ToIdObjects(JToken root)
    {
        return root.TopDescendantsWhere<JObject>(o => o["T_id"] != null)
            .Select(o => new KeyIdObject { key = ((JProperty)o.Parent).Name, T_id = (long)o["T_id"], Children = ToIdObjects(o) })
            .ToList();
    }
}

The List<KeyIdObject> contents looks something like this below:

Output List<KeyIdObject>: 
[
  {
    "key": "Soccer",
    "T_id": 0,
    "Children": [
      {
        "key": "Clubs",
        "Children": [
          {
            "key": "ClubA",
            "T_id": 1
          },
          {
            "key": "ClubB",
            "T_id": 2
          }
        ]
      },
      {
        "key": "Subs",
        "Children": [
          {
            "key": "SubA",
            "T_id": 3,
            "Children": [
              {
                "key": "SubE",
                "T_id": 3
              }
            ]
          }
        ]
      },
      {
        "key": "Subs_Used",
        "Children": [
          {
            "key": "SubK",
            "T_id": 3
          }
        ]
      }
    ]
  }
]

From this information, I want to be able to parse each "key" and its children and build a view like the following when the user clicks on the respective key, there will will be drop-down list with the respective contents.

>Soccer
  >Clubs
    ClubA
    ClubB
  >Subs
    SubA
  >Subs_Used
    SubK

I thought it would be good to create two ObservableCollection as follows.

  1. ObservableCollection<KeyIdObject> rootMenuItem; //to hold just the "Soccer" string and its "T_id"?
  2. ObservableCollection<KeyIdObject> subMenuItem; //append this collection as the child to the rootMenuItem in the end after done populating

When I iterate over the resulting outputlist, I want to maybe use like a recursive function that can add all the "children" in the hierarchy as shown in the Output List<KeyIdObject> to produce something like this below:

  >Clubs
    ClubA
    ClubB
  >Subs
    SubA
  >Subs_Used
    SubK

I am not sure on how to create the recursive function which will do this. Any guidance would be highly appreciated. I am not sure if this is the most efficient way to do it or is there a better way.

I am using the SfTreeView from https://help.syncfusion.com/xamarin/sftreeview/getting-started

dbc
  • 104,963
  • 20
  • 228
  • 340
Laura Smith
  • 293
  • 3
  • 13
  • @dbc I just posted the new question in relation to the one I asked yesterday. – Laura Smith May 31 '19 at 12:12
  • @dbc I am using xamarin forms and trying to create a ui which can be shared across ios, android, and windows platforms. Thats why I am binding the itemsource of my list to the Observable collection and the text to the 'key'. But I am not able to create the nested-like structure. It is simply flat.:/ – Laura Smith May 31 '19 at 17:28
  • @dbc They have a treeview and it is [https://help.syncfusion.com/xamarin/sftreeview/getting-started]. But I am not sure on how to recursively populate this. – Laura Smith May 31 '19 at 17:44
  • @dbc May you please help me to create this treeview. I am doing my best but I do not know the way out. Thank you for the guidance in the meanwhile. – Laura Smith May 31 '19 at 18:19
  • @dbc I have been trying but have not succeeded in the past one hour. I am completely lost and would appreciate any help. Thank you. – Laura Smith May 31 '19 at 20:03
  • 1
    Unfortunately I have never used https://help.syncfusion.com/xamarin/sftreeview/getting-started and don't have it installed, so I can't answer. But from reading the docs you have two options: 1) Manually populate all the tree nodes recursively in the same way as a winforms TreeView, or 2) Bind in hierarchical data in the same way as a WPF TreeView. – dbc May 31 '19 at 21:03
  • 2
    For an example of manual node creation in winforms, see [How to recursively populate a TreeView with JSON data](https://stackoverflow.com/q/39673815/3744182). – dbc May 31 '19 at 21:04
  • 1
    For an example of automatic binding that just works with a WPF TreeView, see [how to bind dynamic json into treeview wpf](https://stackoverflow.com/q/23812357/3744182). Note that you will need to make your data model implement `INotifyPropertyChanged` an use `ObservableCollection`. I updated [this answer](https://stackoverflow.com/a/56385455/3744182) to show an example of that. Good luck. – dbc May 31 '19 at 21:05
  • 1
    Also, you might edit your question to clean up all the references to JSON and Json.NET, and just show your data model. As it is, experts in [tag:xamarin.forms] might skip the question because of the need to plow though all the JSON, which doesn't really matter here. For more see https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/ and https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/: *it’s up to you to do all you can to make that as simple as possible.* – dbc May 31 '19 at 21:07
  • @dbc I have added some comments in the this link, [https://dotnetfiddle.net/IPAOHv]. I am a bit confused on how to do it. May you please guide me in the right direction. Thank you so much for your help in the meanwhile. – Laura Smith Jun 03 '19 at 16:49
  • @dbc I was wondering if you could give me some insight. – Laura Smith Jun 04 '19 at 13:38

0 Answers0