简体   繁体   中英

Structuring data hierarchy in JSON

I am trying to write the following data structure in JSON. Each item is a generic string of text. I cannot get my head around if this is an "arrays inside arrays" situation or "objects inside objects". How should I structure it?

EDIT to create context: All the data are bits of text from a website (links, descriptions, etc.), that needs to be translated into different languages. This means that every JSON-key will be unique.

Here are the dummy data:

1       
 1.1    
    1.1.1
    1.1.2
    1.1.3

 1.2    
 1.3    
 1.4    

2       
 2.1    
    2.1.1
    2.1.2
    2.1.3
    2.1.4
    2.1.5
    2.1.6
    2.1.7
    2.1.8
    2.1.9
    2.1.10

 2.2    
    2.2.1
    2.2.2
    2.2.3
    2.2.4

 2.3    
    2.3.1
    2.3.2
    2.3.3

3       
 3.1    
    3.1.1
    3.1.2
    3.1.3
    3.1.4
    3.1.5
    3.1.6
    3.1.7
    3.1.8
    3.1.9

 3.2    
    3.2.1
    3.2.2
    3.2.3
    3.2.4
    3.2.5
    3.2.6
    3.2.7
    3.2.8
    3.2.9

4       
 4.1    
 4.2    
    4.2.1
    4.2.2
    4.2.3
    4.2.4
    4.2.5
    4.2.6
    4.2.7
    4.2.8
    4.2.9
    4.2.10

5       
 5.1    

I would suggest you to store json tree something like this

[
  {
    "text": "Parent 1",
    "id"  : "1",
    "nodes": [
      {
        "text": "Child 1",
        "id"  : "2",
        "parentid"  : "1",
        "nodes": [
          {
            "text": "Grandchild 1",
            "id"  : "4",
            "parentid"  : "2",
          },
          {
            "text": "Grandchild 2",
             "id"  : "8",
            "parentid"  : "2",
          }
        ]
      },
      {
        "text": "Child 2",
        "id"  : "10",
        "parentid"  : "1",
      }
    ]
  },
  {
    "text": "Parent 2",
    "id"  : "19",
    //no parent id
  }
];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM