首页 > 解决方案 > 将餐厅菜单数据转换为 json

问题描述

我拥有的数据格式-

Beverages
    Coffee -
        Cafe Latte
            Hot         90
            Iced        120
                Vegan-       50
                    Soy Milk     () 
                    Coconut Milk () 
        Cafe Mocha
            Hot         100
            Iced        130
                Vegan-       50
                    Soy Milk     () 
                    Coconut Milk () 
        Cafe Cappucinno      
            Hot         90
            Iced        120
                Vegan-       50
                    Soy Milk     () 
                    Coconut Milk () 
        Cafe Zenzero        100
            Vegan-       50
                Soy Milk     ()
                Coconut Milk () 
        Cafe Americano       50
        Cafe Espresso        50

如何将此餐厅菜单数据转换为 json

我能赚这么多,但不知道如何分类:

{
   "Beverages":{
      "Coffee":{
         "Cafe Latte":{
            "Hot":{
               "price":90
            },
            "Iced":{
               "price":110
            }
         }
      }
   }
}

有一个更好的方法吗?我做错了吗,我对此很陌生

标签: json

解决方案


我的建议可以帮助您入门:
饮料应该是一系列饮料对象。每个饮料都是一个对象,并具有各种属性,例如namepriceoptions等。如果有多个选项,则属性的值也应该是一个对象数组。

我建议您查看有关对象的课程,以进一步巩固您的理解。

const beverages = [
 {
    name: 'Cafe Latte',
    category: 'coffee',
    options: [
      {
        name: 'Hot',
        price: 90,
      },
      {
        name: 'Iced',
        price: 120,
      },
    ],
    additionalOptions: [
      {
        name: 'Vegan',
        price: 50,
        options: [
          {
            name: 'Soy Milk',
          },
          {
            name: 'Coconut Milk',
          },
        ],
      }
    ],
  },
];

推荐阅读