正确使用 JSON 进行简单的餐厅菜单描述

发布于 2024-12-07 10:58:23 字数 624 浏览 0 评论 0原文

我想知道我制作的这个 JSON 脚本示例的格式是否正确,以及这样放置信息是否有意义。

{"menu": {
  "drinks": [
    {"coke": "20"},
    {"pepsi": "20"},
    {"water": "20"}
  ],
  "junk-food": [
    {"hamburger": "40"},
    {"fries": "20"},
    {"pizza": "20"}
  ]
}}

我已经使用 http://jsonlint.com/ 验证了脚本,但我仍然想多做一点,因为我我很新。

对于脚本使用的一些上下文,我将使用 Python 解析脚本。

它的目的是组织 GUI 的元素,这些元素看起来或多或少像这样:

在此处输入图像描述

在第二个窗口上,将出现一个与第一个类似的列表框,其中包含相应的商品和相应的价格。

  • JSON 正确吗?
  • 这个结构有意义吗?

I would like to know whether this JSON script example I made is well formatted and whether it does make sense to put the information like this.

{"menu": {
  "drinks": [
    {"coke": "20"},
    {"pepsi": "20"},
    {"water": "20"}
  ],
  "junk-food": [
    {"hamburger": "40"},
    {"fries": "20"},
    {"pizza": "20"}
  ]
}}

I already validated the script with http://jsonlint.com/ but still I would like to a little more since I'm very new.

For some context on the use of the script, I'm going to parse the script with Python.

It is meant to organize elements of a GUI that will look more or less like this:

enter image description here

On the second window, a listbox similar to the first one will appear with the corresponding item and respective price.

  • Is it correct JSON?
  • Does this structure make sense?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

影子的影子 2024-12-14 10:58:23

JSON 是正确的。但该结构没有太多语义意义。我更改了结构,使其具有更多含义,并且在添加属性时更易于管理。

{"menu": {
    "items": [
        {
            "name":"coke",
            "qty": 20,
            "category":"drinks",
            "sizes":["small","large"]
        },
        {
            "name":"pepsi",
            "qty": 20,
            "category":"drinks",
            "sizes":["small","large"]
        },
        {
            "name":"water",
            "qty": 20,
            "category":"drinks",
            "sizes":["small","large"]
        },
        {
            "name":"hamburger",
            "qty": 40,
            "category":"junk food",
            "sizes":["small","large"]
        },
        {
            "name":"fries",
            "qty": 20,
            "category":"junk food",
            "sizes":["small","large"]
        },
        {
            "name":"pizza",
            "qty": 20,
            "category":"junk food",
            "sizes":["small","large"]
        }
    ]
}}

为了节省空间你也可以这样做,

{"menu": {
    "items": [
        {
            "name":"coke",
            "qty": 20,
            "category":0,
            "sizes":["small","large"]
        },
        {
            "name":"pepsi",
            "qty": 20,
            "category":0,
            "sizes":["small","large"]
        },
        {
            "name":"water",
            "qty": 20,
            "category":0,
            "sizes":["small","large"]
        },
        {
            "name":"hamburger",
            "qty": 40,
            "category":1,
            "sizes":["small","large"]
        },
        {
            "name":"fries",
            "qty": 20,
            "category":1,
            "sizes":["small","large"]
        },
        {
            "name":"pizza",
            "qty": 20,
            "category":1,
            "sizes":["small","large"]
        }
    ],
    "categories":[
        "drinks",
        "junk food"
    ]
}}

The JSON is correct. The structure doesn't have much semantic meaning though. I changed the structure so that it has more meaning and will be more manageable when attributes are added.

{"menu": {
    "items": [
        {
            "name":"coke",
            "qty": 20,
            "category":"drinks",
            "sizes":["small","large"]
        },
        {
            "name":"pepsi",
            "qty": 20,
            "category":"drinks",
            "sizes":["small","large"]
        },
        {
            "name":"water",
            "qty": 20,
            "category":"drinks",
            "sizes":["small","large"]
        },
        {
            "name":"hamburger",
            "qty": 40,
            "category":"junk food",
            "sizes":["small","large"]
        },
        {
            "name":"fries",
            "qty": 20,
            "category":"junk food",
            "sizes":["small","large"]
        },
        {
            "name":"pizza",
            "qty": 20,
            "category":"junk food",
            "sizes":["small","large"]
        }
    ]
}}

to save space you could do something like this too,

{"menu": {
    "items": [
        {
            "name":"coke",
            "qty": 20,
            "category":0,
            "sizes":["small","large"]
        },
        {
            "name":"pepsi",
            "qty": 20,
            "category":0,
            "sizes":["small","large"]
        },
        {
            "name":"water",
            "qty": 20,
            "category":0,
            "sizes":["small","large"]
        },
        {
            "name":"hamburger",
            "qty": 40,
            "category":1,
            "sizes":["small","large"]
        },
        {
            "name":"fries",
            "qty": 20,
            "category":1,
            "sizes":["small","large"]
        },
        {
            "name":"pizza",
            "qty": 20,
            "category":1,
            "sizes":["small","large"]
        }
    ],
    "categories":[
        "drinks",
        "junk food"
    ]
}}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文