简体   繁体   English

在JSON中构建数据层次结构

[英]Structuring data hierarchy in JSON

I am trying to write the following data structure in JSON. 我正在尝试用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. 这意味着每个JSON键都是唯一的。

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 我建议您存储json这样的树

[
  {
    "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
  }
];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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