JSON multiple structs reading

Hello, I’ am trying to reach specific question parameters whenever I want. But I am getting an error even though I made the connection from Unity.


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

public class JSONGReader : MonoBehaviour
{
    string jsonString;
    public TextAsset jsonTextAsset;

    [System.Serializable]
    public struct QuestionObject
    {
        public string categoryType;
        public string type;
        public string difficulty;
        public string question;
        public string correctAnswer;
        public string[] incorrectAnswers;

    }

    void Start()
    {
        ReadData();
    }

    private void ReadData()
    {
        jsonString = jsonTextAsset.text;
        QuestionObject[] results = JsonHelper.getJsonArray<QuestionObject>(jsonString);
        foreach (QuestionObject a in results)
        {
            Debug.Log(a.categoryType);
        }
    }
}

public class JsonHelper
{
    public static T[] getJsonArray<T>(string json)
    {
        string newJson = "{ \"array\": " + json + "}";
        Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(newJson);
        return wrapper.array;
    }

    [System.Serializable]
    private class Wrapper<T>
    {
        public T[] array;
    }
}
{
  "response_code": 0,
  "results": [
    {
      "category": "General Knowledge",
      "type": "multiple",
      "difficulty": "medium",
      "question": "What name represents the letter "M" in the NATO phonetic alphabet?",
      "correct_answer": "Mike",
      "incorrect_answers": [ "Matthew", "Mark", "Max" ]
    },
    {
      "category": "General Knowledge",
      "type": "multiple",
      "difficulty": "medium",
      "question": "Which Italian automobile manufacturer gained majority control of U.S. automobile manufacturer Chrysler in 2011?",
      "correct_answer": "Fiat",
      "incorrect_answers": [ "Maserati", "Alfa Romeo", "Ferrari" ]
    },
    {
      "category": "General Knowledge",
      "type": "multiple",
      "difficulty": "medium",
      "question": "What is the name of the popular animatronic singing fish prop, singing such hits such as "Don't Worry, Be Happy"?",
      "correct_answer": "Big Mouth Billy Bass",
      "incorrect_answers": [ "Big Billy Bass", "Singing Fish", "Sardeen" ]
    },
    {
      "category": "General Knowledge",
      "type": "multiple",
      "difficulty": "medium",
      "question": "What is the last letter of the Greek alphabet?",
      "correct_answer": "Omega",
      "incorrect_answers": [ "Mu", "Epsilon", "Kappa" ]
    },
    {
      "category": "General Knowledge",
      "type": "multiple",
      "difficulty": "medium",
      "question": "Which iconic Disneyland attraction was closed in 2017 to be remodeled as a "Guardians of the Galaxy" themed ride?",
      "correct_answer": "Twilight Zone Tower of Terror",
      "incorrect_answers": [ "The Haunted Mansion", "Pirates of the Caribbean", "Peter Pan's Flight" ]
    },
    {
      "category": "General Knowledge",
      "type": "multiple",
      "difficulty": "medium",
      "question": "What is real haggis made of?",
      "correct_answer": "Sheep's Heart, Liver and Lungs",
      "incorrect_answers": [ "Sheep's Heart, Kidneys and Lungs", "Sheep's Liver, Kidneys and Eyes", "Whole Sheep" ]
    },
    {
      "category": "General Knowledge",
      "type": "multiple",
      "difficulty": "medium",
      "question": "Where did the pineapple plant originate?",
      "correct_answer": "South America",
      "incorrect_answers": [ "Hawaii", "Europe", "Asia" ]
    },
    {
      "category": "General Knowledge",
      "type": "multiple",
      "difficulty": "medium",
      "question": "Directly between the Washington Monument and the Reflecting Pool is a memorial to which war?",
      "correct_answer": "World War II",
      "incorrect_answers": [ "Vietnam War", "American Civil War", "American Revolutionary War" ]
    },
    {
      "category": "General Knowledge",
      "type": "multiple",
      "difficulty": "medium",
      "question": "The Mexican Beer "Corona" is what type of beer?",
      "correct_answer": "Pale Lager",
      "incorrect_answers": [ "India Pale Ale", "Pilfsner", "Baltic Porter" ]
    },
    {
      "category": "General Knowledge",
      "type": "multiple",
      "difficulty": "medium",
      "question": "What are the three starter Pokemon available in Pokemon Black and White?",
      "correct_answer": "Snivy, Tepig, Oshawott",
      "incorrect_answers": [ "Snivy, Fennekin, Froakie", "Chespin, Tepig, Froakie", "Chespin, Fennekin, Oshawott" ]
    }
  ]
}

I guess line 44 does not set the array in wrapper like you expect it to.

There are sites on the internet where you can test your json.
Please test the string produced by "{ \"array\": " + json + "}";

Like you said problem was that. Thank you. With foreach I can call the information of every question. But how can I call for example 5th question’s correct answer? Still couldn’t figure that out.

The problem is when I call any category, everything is ok. But when I try to call correctAnswer result is Null.

So I found out that the variable name at JSON has to be the same as at the class. Sorry I am new to JSON stuff :slight_smile:

You may wish to consider using a “real” JSON instead of the built-in JSON.

Problems with Unity “tiny lite” built-in JSON:

In general I highly suggest staying away from Unity’s JSON “tiny lite” package. It’s really not very capable at all and will silently fail on very common data structures, such as Dictionaries and Hashes and ALL properties.

Instead grab Newtonsoft JSON .NET off the asset store for free, or else install it from the Unity Package Manager (Window → Package Manager).

Also, always be sure to leverage sites like:

https://csharp2json.io

1 Like