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.

6
  • It would be easier to help you if you'd provide a minimal reproducible example. Commented Mar 10, 2017 at 3:26
  • Hey John, yes this link is provided above dotnetfiddle.net/I4sAGZ, thanks! Commented Mar 10, 2017 at 3: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. Commented Mar 10, 2017 at 3: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... Commented Mar 10, 2017 at 5:34
  • Just posted and answer take a look if that works for you Commented Mar 10, 2017 at 6:08

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.