关于如何将数据放入JSON格式的问题。

huangapple go评论74阅读模式
英文:

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

{
&quot;correctAnswers&quot;: {
  &quot;category&quot;: {
    &quot;name&quot; : &quot;Europe&quot;, 
    &quot;categoryAnswers&quot;: [&quot;Germany&quot;, &quot;Austria&quot;, &quot;Hungary&quot;]
  }
 }
}

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

{
  &quot;correctAnswers&quot;: {
    &quot;categories&quot;: [
      {
        &quot;name&quot; : &quot;Europe&quot;, 
        &quot;categoryAnswers&quot;: [&quot;Germany&quot;, &quot;Austria&quot;, &quot;Hungary&quot;]
      }
    ]
  }
}

Which would then allow you to add more "category" objects to that array:

{
  &quot;correctAnswers&quot;: {
    &quot;categories&quot;: [
      {
        &quot;name&quot; : &quot;Europe&quot;, 
        &quot;categoryAnswers&quot;: [&quot;Germany&quot;, &quot;Austria&quot;, &quot;Hungary&quot;]
      },
      {
        &quot;name&quot; : &quot;Asia&quot;, 
        &quot;categoryAnswers&quot;: [&quot;China&quot;, &quot;Japan&quot;, &quot;Cambodia&quot;]
      }
    ]
  }
}

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": ["中国", "日本", "印度"]
    }
}

}

英文:
{
    &quot;correctAnswers&quot;: {
        &quot;Europe&quot;: {
           &quot;name&quot;: &quot;Europe&quot;,
           &quot;categoryAnswers&quot;: [&quot;Germany&quot;, &quot;Austria&quot;, &quot;Hungary&quot;]
         },
 
        &quot;Asia&quot;: {
           &quot;name&quot;: &quot;Asia&quot;,
           &quot;categoryAnswers&quot;: [&quot;China&quot;, &quot;Japan&quot;, &quot;India&quot;]
         }
     }
}

答案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 -->

{
  &quot;correctAnswers&quot;: {
    &quot;categories&quot;: [
      {
        &quot;category&quot;: {
          &quot;id&quot;: 1,
          &quot;name&quot;: &quot;Europe&quot;,
          &quot;categoryAnswers&quot;: [&quot;Germany&quot;, &quot;Austria&quot;, &quot;Hungary&quot;]
        }
      },
      {
        &quot;category&quot;: {
          &quot;id&quot;: 2,
          &quot;name&quot;: &quot;Asia&quot;,
          &quot;categoryAnswers&quot;: [&quot;China&quot;, &quot;Japan&quot;, &quot;Cambodia&quot;]
        }
      }
    ]
  }
}

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月6日 21:27:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76629354.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定