0

I looked at this SO question but it doesn't address my needs: Build JSON Hierarchy from Structured Data

The problem: When trying to build the json, the nesting of children isn't done correctly. The children property should contain 1 or more name array items like this:

  "children": [{
    "name": "XXX - Level XXX",

...instead it is generated as:

  "children": []

Here's a dotnet fiddle with more code details:

I'm trying to build the tree by using json.net .Last to grab the last child and then add a JObject to that child but that isn't working out too well for me.

The main data structure used to build the json :

  Dictionary<int, Industry>();

The desired json structure should be:

  {
    "name": "XX Level XX",
    "children": [{
        "name": "XXX - Level XXX",
        "children": [{
            "name": "XXXX - Level XXXX",
            "children": [{
                "name": "XXXXX - Level XXXXX",
                "children": [{
                    "name": "XXXXXX - Level XXXXXX"
                 }]
            }]
        }]
     }]
 }

The actual output is:

  {
    "name": "XX-Level XX",
    "children": [{
                 "name": "XXX-Level XXX",
                 "children": []
                 }, {
                 "name": "XXXX-Level XXXX",
                 "children": []
                 }, {
                 "name": "XXXXX-Level XXXXX",
                 "children": []
                 }, {
                 "name": "XXXXXX-Level XXXXXX",
                  "children": []
                 }
               ]
     }     

Here's the code that builds the json:

    dynamic relationship = new JObject();
    relationship.name = relationships[0].Industries[1].Name;
    relationship.children = new JArray();

    var lftIndustries = relationships[0].Industries.Where(k => k.Key > 1);

    foreach (var item in lftIndustries)
    {
            //not good enough, need to get deepest, empty "children" node
            // and add "industry" to it
            var node = ((JContainer)relationship).Last;
            var childArray = (JArray)((JContainer) node).Last;

            var industry = new JObject(new JProperty("name", item.Value.Name), new JProperty("children", new JArray()));

            childArray.Add(industry);
    }

    json = relationship.ToString(); 

I thought that using json.net .Last and .Next would be the answer.

Community
  • 1
  • 1
Will Lopez
  • 2,089
  • 3
  • 40
  • 64
  • It would be easier to help you if you'd provide a [mcve]. – Jon Skeet Mar 10 '17 at 03:26
  • Hey John, yes this link is provided above https://dotnetfiddle.net/I4sAGZ, thanks! – Will Lopez Mar 10 '17 at 03:32
  • No, a) that doesn't seem minimal (at the very least it could be formatted considerably more compactly, and I doubt you need that many properties); b) it should be in the question. – Jon Skeet Mar 10 '17 at 03:47
  • I can see children attribute is always and empty array because you never add any items to it... al lest is what i can see... – Jav T Mar 10 '17 at 05:34
  • Just posted and answer take a look if that works for you – Jav T Mar 10 '17 at 06:08
  • @JavTThis is close, the last node doesn't have any children so shouldn't have an array object (I know it's coded that way :-)). I'll have to do a check if it's the last one. That's close enough for an answer, Thanks! – Will Lopez Mar 10 '17 at 14:16

1 Answers1

0

try this in your foreach loop and tell me if it is whats youre looking for:

JToken currentContainer = null;
foreach (var item in lftIndustries)
        {

                //not good enough, need to get deepest, empty "children" node
                // and add "industry" to it
            if (currentContainer != null)
            {
                var node = ((JContainer)currentContainer).Last;
                var childArray = (JArray)((JContainer) node).Last;

                var industry = new JObject(new JProperty("name", item.Value.Name), new JProperty("children", new JArray()));

                childArray.Add(industry);
                currentContainer = industry;
            }
            else
            {
                var node = ((JContainer)relationship).Last;
                var childArray = (JArray)((JContainer) node).Last;

                var industry = new JObject(new JProperty("name", item.Value.Name), new JProperty("children", new JArray()));

                childArray.Add(industry);
                currentContainer = industry;
            }


        }

The result is this:

{
  "name": "XX-Level XX",
  "children": [
    {
      "name": "XXX-Level XXX",
      "children": [
        {
          "name": "XXXX-Level XXXX",
          "children": [
            {
              "name": "XXXXX-Level XXXXX",
              "children": [
                {
                  "name": "XXXXXX-Level XXXXXX",
                  "children": []
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

Heres the fiddle: https://dotnetfiddle.net/1yzTGU

Jav T
  • 478
  • 3
  • 12