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?
-
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 bejunk-food
and also aside-dish
andappetizer
. – 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
1 Answer
Reset to default 5The 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条)