skip to Main Content

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:

{
"correctAnswers": {
  "category": {
    "name" : "Europe", 
    "categoryAnswers": ["Germany", "Austria", "Hungary"]
  }
 }
}

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.

3

Answers


  1. {
        "correctAnswers": {
            "Europe": {
               "name": "Europe",
               "categoryAnswers": ["Germany", "Austria", "Hungary"]
             },
     
            "Asia": {
               "name": "Asia",
               "categoryAnswers": ["China", "Japan", "India"]
             }
         }
    }
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
  3. You can put something like this, if behind you want to gather it in some list of objects :

    {
      "correctAnswers": {
        "categories": [
          {
            "category": {
              "id": 1,
              "name": "Europe",
              "categoryAnswers": ["Germany", "Austria", "Hungary"]
            }
          },
          {
            "category": {
              "id": 2,
              "name": "Asia",
              "categoryAnswers": ["China", "Japan", "Cambodia"]
            }
          }
        ]
      }
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search