Using a JSON file as a datasource

So i can understand why he hard coded the questions and all the answers in since this is just a small game with around 20 30 or maybe 40 questions tops. But what if I want to make a game that has thousands of story parts with many changes that will be made in the future, given the situation, is there a way that I can load data from my JSON data file into the text field of the UI? maybe I don’t have to type things in the questions text input and answers no more but I only have to point out which question this is in a JSON file and let Unity do the rest.

For example, I have this JSON file right here, how to i poor it into the game UI?

{
  "id": "a_cat_and_a_bat",
  "story": "A Cat and a Bat",
  "sentences": [
    "A Cat and a Bat.",
    "A phonics story is told by Sunny Friends.",
    "A playful cat wears a big hat.",
    "He wants to eat apple cake.",
    "The hat is too large, so he cannot climb the tree.",
    "A bat appears and says,\"I will help.\"",
    "The bat grabs the apple.",
    "The cat makes an apple cake.",
    "The bat and the cat share the apple cake happily."
  ],
  "noun": [
    "cat",
    "bat",
    "hat",
    "apple",
    "cake",
    "tree"
  ],
  "question": [
    {
      "q": "What does the cat wear?",
      "a": [
        "A bat",
        "A hat",
        "An apple",
        "A cake"
      ],
      "c": 1
    },
    {
      "q": "Why can the cat not climb the tree?",
      "a": [
        "The cat is hungry",
        "The bat doesn't help",
        "The hat is too large",
        "The tree is very tall"
      ],
      "c": 2
    },
    {
      "q": "What does the bat help the cat?",
      "a": [
        "The bat picks the apple",
        "The bat eats the apple cake",
        "The bat helps the cat to climb",
        "The bat flies with the cat"
      ],
      "c": 0
    },
    {
      "q": "What do the can and the cat share together?",
      "a": [
        "An apple",
        "An apple cake",
        "A hat",
        "An apple tree"
      ],
      "c": 1
    }
  ]
}

Hi,

Have you already done some research on the internet?

I hope this helped. :slight_smile:


See also:

I wrote a post on how to get trivia questions from the web. These questions are in json form and the post includes how to parse and use it.
See the post here:


Edit
In your case you don’t need the WebAPI bits, so you can reference your json via a TextAsset type and just drag the file in the inspector, and then read it in the code. See the documentation

nice tutorial btw, but im wondering, what if I don’t have to call from a public API but I already have a json file as a data source myself, what to do then? Since in your tutorial, it’s calling and reading from a public source, I have a JSON file and I wanted to load data from that file I’m having into the game UI, is there a way? so later on if i wanna change something, I just have to change it in the json file then the game automatically follows up,
Here is a JSON file that I’m having


  "question": [
    {
      "q": "What does the cat wear?",
      "a": [
        "A bat",
        "A hat",
        "An apple",
        "A cake"
      ],
      "c": 1
    },
    {
      "q": "Why can the cat not climb the tree?",
      "a": [
        "The cat is hungry",
        "The bat doesn't help",
        "The hat is too large",
        "The tree is very tall"
      ],
      "c": 2
    },
    {
      "q": "What does the bat help the cat?",
      "a": [
        "The bat picks the apple",
        "The bat eats the apple cake",
        "The bat helps the cat to climb",
        "The bat flies with the cat"
      ],
      "c": 0
    },
    {
      "q": "What do the can and the cat share together?",
      "a": [
        "An apple",
        "An apple cake",
        "A hat",
        "An apple tree"
      ],
      "c": 1
    }
  ]
}

I edited the post. You can use a TextAsset type in the inspector

[SerializeField] TextAsset questionsFile;

Then you can skip all the web stuff up to the point where it has already received the data and use the file instead. You’d have to create the data structs specifically for your json and then just use the JsonUtility to parse it

string jsonString = questionsFile.text; // read the questions from the file
Debug.Log(jsonString);
QuestionsModel questions = JsonUtility.FromJson<QuestionsModel>(jsonString);

So, you can follow the web post but skip the web bits and just read the data from the file

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms