英文:
Question about how to put data in JSON format
问题
{
"correctAnswers": {
"category": {
"name": "Asia",
"categoryAnswers": ["China", "Japan", "Cambodia"]
}
}
}
英文:
Hello i have categories with answers data, e.g.:
- Europe: Germany, Austria, Hungary
- Asia: China, Japan, Cambodia
And I need to put it in json:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
{
"correctAnswers": {
"category": {
"name" : "Europe",
"categoryAnswers": ["Germany", "Austria", "Hungary"]
}
}
}
<!-- end snippet -->
How i can add the second category Asia with answers?
I understand that my question is very simple and my suggested variant may be not optimal but i am total new in Json and haven't get the right answer in Google.
答案1
得分: 2
数据结构的关键在于您所描述的数据的语义。您一直在使用这个词(在问题和评论中):
categories
但是您的结构中有这个词:
category
这可能是您要寻找的区别。如果“category”应该表示“多样性”,那么我想它应该是一个数组:
{
"correctAnswers": {
"categories": [
{
"name" : "Europe",
"categoryAnswers": ["Germany", "Austria", "Hungary"]
}
]
}
}
然后您可以向该数组添加更多的“category”对象:
{
"correctAnswers": {
"categories": [
{
"name" : "Europe",
"categoryAnswers": ["Germany", "Austria", "Hungary"]
},
{
"name" : "Asia",
"categoryAnswers": ["China", "Japan", "Cambodia"]
}
]
}
}
最终,这取决于您以及您希望如何为应用程序结构化数据。数据结构本质上是一系列可以无限嵌套的:
- 数组
- 对象
- 值
因此,如果从语义上讲,“correctAnswers”对象应该具有“category”对象的列表,那么从语义上讲,该列表将被称为“categories”并且将是一个数组。这真的取决于您试图在结构中描述的数据的语义。
英文:
The data structure is all in the semantics of the data you're describing. You keep using this word (in the question and comment(s)):
> categories
But your structure has this word:
> category
That's probably the difference you're looking for. If category
should represent plurality then I would imagine it should be an array:
{
"correctAnswers": {
"categories": [
{
"name" : "Europe",
"categoryAnswers": ["Germany", "Austria", "Hungary"]
}
]
}
}
Which would then allow you to add more "category" objects to that array:
{
"correctAnswers": {
"categories": [
{
"name" : "Europe",
"categoryAnswers": ["Germany", "Austria", "Hungary"]
},
{
"name" : "Asia",
"categoryAnswers": ["China", "Japan", "Cambodia"]
}
]
}
}
Ultimately it's up to you and how you want to structure the data for the application. The data structure is essentially an endlessly nestable series of:
- Arrays
- Objects
- Values
So if, semantically, the correctAnswers
object should have a list of category
objects, then semantically that list would be called categories
and would be an array. It's really up to the semantics of the data you're trying to describe in the structure.
答案2
得分: 1
{
"correctAnswers": {
"Europe": {
"name": "欧洲",
"categoryAnswers": ["德国", "奥地利", "匈牙利"]
},
"Asia": {
"name": "亚洲",
"categoryAnswers": ["中国", "日本", "印度"]
}
}
}
英文:
{
"correctAnswers": {
"Europe": {
"name": "Europe",
"categoryAnswers": ["Germany", "Austria", "Hungary"]
},
"Asia": {
"name": "Asia",
"categoryAnswers": ["China", "Japan", "India"]
}
}
}
答案3
得分: 1
你可以这样做,如果你想将它收集到一些对象列表中:
{
"correctAnswers": {
"categories": [
{
"category": {
"id": 1,
"name": "Europe",
"categoryAnswers": ["Germany", "Austria", "Hungary"]
}
},
{
"category": {
"id": 2,
"name": "Asia",
"categoryAnswers": ["China", "Japan", "Cambodia"]
}
}
]
}
}
英文:
You can put something like this, if behind you want to gather it in some list of objects :
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
{
"correctAnswers": {
"categories": [
{
"category": {
"id": 1,
"name": "Europe",
"categoryAnswers": ["Germany", "Austria", "Hungary"]
}
},
{
"category": {
"id": 2,
"name": "Asia",
"categoryAnswers": ["China", "Japan", "Cambodia"]
}
}
]
}
}
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论