代码之家  ›  专栏  ›  技术社区  ›  Vitaly Fadeev

如何在JSON中存储层次结构?

  •  1
  • Vitaly Fadeev  · 技术社区  · 12 年前

    如何在JSON中存储层次结构?

    我有以下层次结构:

    层次结构:

     1:Publish, "Long description of Publish service", is_default
         7: 7 days,  150
        14:14 days,  600, +200%
        30:1 month, 1350, +300%
     2:Premium, "Long description..."
        14:14 days,  150
        30:1 month,  600
        60:2 month,  1500
     3:SuperPremium, "Long description...", disabled
        30:1 month,  150
        60:2 month,  600
        90:3 month,  1500, disabled
    

    其中:

    • “发布,高级,超级高级”-即服务。1,2,3-服务ID。

    • “7天、14天、28天”是服务参数。7,14,28-参数ID。

    • 参数取决于服务。

    下图对此进行了说明:

    enter image description here

    需要lite和JSON格式的可用结构。

    用JSON表示这种层次结构的最佳方式是什么?

    6 回复  |  直到 12 年前
        1
  •  3
  •   TheZ    12 年前

    我会做一些类似的事情:

    [
       {
          "Service":"Publish",
          "Desc":"Long description of Publish serive",
          "Params":[
             {
                "Days":"7",
                "Desc":"7 Days",
                "Cost":150
             },
             {
                "Days":"14",
                "Desc":"14 Days",
                "Cost":600,
                "Extra":"+200%"
             },{...}
          ]
       }, {...}, {...}
    ]
    
        2
  •  2
  •   Matthew Cox    12 年前

    我的方法:

    {
            'data' : [
            {
                'ServiceId' : 1
                'Name': 'Publish',
                'LongName': 'Long description of Publish service',
                'OptionalFlag': 'default',
                'Parameters': [
                    { 'Id': 7, 'Amount': 7, 'Unit': 'days', 'Cost': 150, 'Surcharge': 0.0 },
                    { 'Id': 14, 'Amount': 14, 'Unit': 'days', 'Cost': 600, 'Surcharge': 2.0 },
                    { 'Id': 30, 'Amount': 1, 'Unit': 'month', 'Cost': 1350, 'Surcharge': 3.0 },
                ]
            },
            {
                'ServiceId' : 2
                'Name': 'Premium',
                'LongName': 'Long description of Publish service',
                'OptionalFlag': 'disabled',
                'Parameters': [
                    { 'Id': 14, 'Amount': 14, 'Unit': 'days', 'Cost': 150, 'Surcharge': 0.0 },
                    { 'Id': 30, 'Amount': 1, 'Unit': 'month', 'Cost': 600, 'Surcharge': 0.0 },
                    { 'Id': 60, 'Amount': 2, 'Unit': 'month', 'Cost': 1500, 'Surcharge': 0.0 },
                ]
            },
            {
                'ServiceId' : 3
                'Name': 'SuperPremium',
                'LongName': 'Long description of Publish service',
                'OptionalFlag': 'disabled',
                'Parameters': [
                    { 'Id': 30, 'Amount': 1, 'Unit': 'month', 'Cost': 150, 'Surcharge': 0.0 },
                    { 'Id': 60, 'Amount': 2, 'Unit': 'month', 'Cost': 600, 'Surcharge': 0.0 },
                    { 'Id': 90, 'Amount': 3, 'Unit': 'month', 'Cost': 1500, 'Surcharge': 0.0 },
                ]
            }
            ]
    }
    

    编辑:(格式化详细信息)

    在代码中有一些格式化细节需要处理,没有任何结构能够绕过这些细节。例如,如何存储百分比附加值,然后应用数学计算总计。我个人喜欢用1.0=100%来代表他们。你还必须以天和月等时间单位来处理多元化问题。

        3
  •  2
  •   Jibi Abraham    12 年前

    JSON可以像编写任何其他JavaScript对象一样编写。以您的示例案例为例

    [
        {
            "publish":{
                 "id":1,
                 "description": "Some description",
                 "text": "Publish",
                 "enabled": true,
                 "params": [
                     {
                        "days": 7,
                        "description": "7 days",
                        "cost": 300
                     },.....//and so on
                 ]
             }
        }
    ]
    
        4
  •  2
  •   Nishant    12 年前
    {
        "publish": {
            "id": 1,
            "desc": "Long description of Publish service",
            "state": 1,
            "params": [
                {
                    "key": 7,
                    "val": 100,
                    "desc": "7 days",
                    "pct": 0,
                    "enabled": true
                },
                {
                    "key": 14,
                    "val": 600,
                    "desc": "14 days",
                    "pct": 200,
                    "enabled": true
                },
                {
                    "key": 30,
                    "val": 1350,
                    "desc": "30 days",
                    "pct": 300,
                    "enabled": true
                }
            ]
        },
        "premimum": {
            ...
        },
        "superPremimum": {
            ...
        }
    }
    

    其中: state 为1:已选择,0:未选择,-1:已禁用

        5
  •  1
  •   Robin Maben    12 年前
    [{
     prop1 : value
     prop2 : {
         level2Prop : value1
         . 
         .         
      }
    },
    {
     ...
    }]
    
        6
  •  0
  •   Vitaly Fadeev    12 年前

    其中一个想法是:

           // default values
           {
            service:'publish', 
            period:7
           }
    
           // data
           {
            service:{  
                     'publish':{title:'Publish'}, 
                     'premium': {title:'Premium',    description: '...'}, 
                     'superpremium':{title:'SuperPremium', description: '...', disabled}
                   },
            period:{ 
                    'publish': {
                              7:{title: '7 days'}, 
                             14:{title:'14 days'}, 
                             28:{title:'28 days'}},
                    'premium': {
                             14:{title:'14 days'}, 
                             28:{title:'28 days'}, 
                             36:{title:'36 days'}},
                    'superpremium': {
                             28:{title:'28 days'}, 
                             60:{title:'60 days'}, 
                             90:{title:'90 days'}}
                   }
           }