javascript - Correct use of JSON for a simple restaurant menu description - Stack Overflow

I would like to know whether this JSON script example I made is well formatted and whether it does make

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 / 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:

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?

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./ 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:

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?
Share Improve this question asked Oct 3, 2011 at 5:27 TrufaTrufa 40.8k44 gold badges132 silver badges193 bronze badges 7
  • I would put all of the items in the same array, with a "category" property- or better yet, an array of category properties. That way fries can be junk-food and also a side-dish and appetizer. – evan Commented Oct 3, 2011 at 5:30
  • How would you model the objects? Do that first. – Derek Beattie Commented Oct 3, 2011 at 5:32
  • @DerekBeattie I'm not sure how to do that really, it is almost my first go with JSON. I will try though. Thanks! – Trufa Commented Oct 3, 2011 at 5:35
  • @evan I don't understand exactly what you are suggesting. If I put every item in an array, how would I identify which category that item is? – Trufa Commented Oct 3, 2011 at 5:39
  • I would give each item an ID as well, and make it clearer what the price is. Like "coke": {id: 1, price: 20}. Also note I removed the "" because it's a number, and would semantically be used better without "". – pimvdb Commented Oct 3, 2011 at 5:42
 |  Show 2 more ments

1 Answer 1

Reset to default 5

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"
    ]
}}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744688144a4588045.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信