Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8413761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:59:06+00:00 2026-06-10T00:59:06+00:00

I have a tree with nodes like this: -Root |-Node 1 |-Node 2 |-Node

  • 0

I have a tree with nodes like this:

-Root
|-Node 1
|-Node 2
  |-Node 2.1

one of these nodes are representet by one class

class Node {
    Integer id;
    String name;
    String route;
}

instances of the shown nodes are saved like

{id: 1, name: "Root", route:"1"}
{id: 2, name: "Node 1", route: "1/2"}
{id: 3, name: "Node 2", route: "1/3"}
{id: 4, name: "Node 2.1", route: "1/3/4"}

the question is: How can i get from a list of nodes to json representing the tree state, e.g.:

[{
"property": {
    "name": "Root",
    "id": "1",
    "route": "1"
},
"children": [{
    "property": {
        "name": "Node 1",
        "id": "2",
        "route": "1/2"
    },
    "property": {
        "name": "Node 2",
        "id": "3",
        "route": "1/3"
    },
    "children": [{
        "property": {
            "name": "Node 3",
            "id": "4",
            "route": "1/3/4"
        }
    }]
}]
}]

i need exactly this json structure

all this stuff have to be done in java. i tried to iterate over my list of nodes and built the json object, but i have problems to get the json structure from the route field of the nodes

i’m able to use a json library the node class is unchangable

EDIT: This format is quite strange, but needed. The “properties” are the nodes, if the “propertie” has children, they are not placed IN the propertie, ether AFTER the propertie… i think thats not quite sensefull but i cant change it

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T00:59:07+00:00Added an answer on June 10, 2026 at 12:59 am

    You could try an algorithm something like this:

    public JSONObject toJSON(Node node, List<Node> others) {
        JSONObject json = new JSONObject();
        json.put("id", node.id); // and so on
        ...
        List children = new ArrayList<JSONObject>();
        for(Node subnode : others) {
            if(isChildOf(subnode, node)) {
                others.remove(subnode);
                children.add(toJSON(subnode, others));
            }
        }
        json.put("children", children);
        return json;
    }
    

    You are modifying a list as you iterate over it, and as recursive calls also iterate over it. This might be hairy, but try it. There are ways around it if it fails.

    isChildOf() is the missing piece. That’s some fairly basic string manipulation, to see whether subnode.path starts with node.id

    Edit: actually this doesn’t create the same structure as in your question. But I can’t make much sense of the structure in your question. However something very similar to this algorithm will produce what you want. The principle is sound.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a tree of nodes like this: class Node: next # the next
I have this very simple C++ class: class Tree { public: Node *head; };
I have a tree, each node contains an array children nodes (Node *children_nodes), as
I have a simple weighted tree with a root, nodes and leafs. Now I
Im having a problem with python.. I have a binary tree node type: class
I have two XML trees and would like to add one tree as a
I have a TreeView with user-editable nodes. I like to use the node caption
I have two tree, which contains nodes, after I've created them, I take all
I have an AdvancedDataGrid structured as a tree. Since the non-leaf nodes have an
I have a binary tree where each node can have a value. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.