简体   繁体   中英

How to create custom JSON data structure?

The output for the JSON data structure with 100 objects should look like this:

[{
    "Value": "Sens1_001",
    "Parent": Null,
    "Child": {
        "Value": "Sens2_068",
        "Parent":"Sens1_001",
        "Child" : {
            "Value": "Sens3_034",
            "Parent": "Sen2_068",
            "Child": null,
            "z_cordinate": 5
                  },
        "z_cordinate": 0
            },
        "z_cordonate": -5
    },
{
    "Value": "Sens1_002",
    "Parent": Null,
    "Child": {
        "Value": "Sens2_037",
        "Parent":"Sens1_002",
        "Child" : {
            "Value": "Sens3_099",
                              "Parent": "Sen2_037",
                              "Child": null,
                              "z_cordinate": 5
                   },
             "z_cordinate": 0
             },
  "z_cordonate": -5
},
{
   "Value": "Sens1_003",
    "Parent": Null,
    "Child": {
        "Value": "Sens2_012",
        "Parent":"Sens1_003",
        "Child" : {
            "Value": "Sens3_054",
            "Parent": "Sen2_012",
            "Child": null,
            "z_cordinate": 5
                  },
        "z_cordinate": 0
             },   
        "z_cordonate": -5
},
.
.
. // till 100th object
{
    "Value": "Sens1_100",
    "Parent": Null,
    "Child": {
        "Value": "Sens2_001",
        "Parent":"Sens1_100",
        "Child" : {
            "Value": "Sens3_021",
            "Parent": "Sen2_001",
            "Child": null,
            "z_cordinate": 5
                  },
    "z_cordinate": 0
             },
    "z_cordonate": -5
}]  

NOTE: I Edited the sample looking object, please review one more time, I changed end braces to look like more an array rather normal object. JSON is secondary, my array should serve my purpose.

Purpose:

How Could I access the objects X.parent.child fashion. I've some knowledge but not enough to structure this object. I'm also planning to access the objects either ways, Grapndparent - Parent - Child and Me - Parent - Grandparent. How could I do that? The objects parent - child relationship should be meaningful rather random and moreover a parent can have more than one child.

Since all the 'value' key are unique, you can have it as the dictionary key. So design it like:

{
 "Sens1_001": {
               "value": "Sens1_001",
               "parent": null,
               "child": "Sen2_068",
               "z_coordinate": 5
              },
 "Sens2_068": {
               "value": "Sens2_068",
               "parent": "Sens1_001",
               "child": "Sen3_098",
               "z_coordinate": -5
              }
 .
 .
 .
}

Once this dictionary 'dict' is ready, if you know the value of the grandparent, then:

grandparent = dict[value];
parent = dict[grandparent[child]];
child = dict[parent[child]];

Similarly if you know the child's value,

child = dict[value];
parent = dict[child[parent]];
grandparent = dict[parent[parent]];

Tell me if you dont get it still.

Since json is itself a javascript array so you can access json object like you access array;

<script>
  var json = [
        {'SMAK' : 1, 'parent': 'i m perent 1', 'child' : 'i m child'}, 
        {'SMAK' : 2, 'parent': 'i m perent 2', 'child' : 'i m child'}, 
        {'SMAK' : 3, 'parent': 'i m perent 3', 'child' : 'i m child'}, 
        {'SMAK' : 4, 'parent': 'i m perent 4', 'child' : 'i m child'}        
      ]
  console.log(json[0]);
  console.log(json[1].parent);
  console.log(json[3]['child']);
</script>

So if you know the index you can access any property of that object;

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