Reputation: 902
I wanna ask you a question about JSON.
I want to differentiate product by categories. How can I write that in JSON?
{"products":
{"category" : "computer"[
{"brand" : "sony"
"price" : "$1000"},
{"brand" : "acer"
"price" : "$400"},]}
,
{"category" : "cell phone"[
{"brand" : "iphone"
"price" : "$800"},
{"brand" : "htc"
"price" : "$500"},]}
}
Upvotes: 0
Views: 2004
Reputation: 208555
I think you may need something like this:
{"products":
{"computer":
[
{"brand" : "sony",
"price" : "$1000"},
{"brand" : "acer",
"price" : "$400"}
],
"cell phone":
[
{"brand" : "iphone",
"price" : "$800"},
{"brand" : "htc",
"price" : "$500"}
]
}
}
By using the category as the key in the JSON object you can easily access all products in that category, for example:
>>> data['products']['computer']
[{'brand': 'sony', 'price': '$1000'}, {'brand': 'acer', 'price': '$400'}]
If necessary, you can also add a list of categories to top level JSON object so that you know what categories are available:
{"products": {...},
"categories": ["computer", "cell phone"]
}
Upvotes: 1