Loading text via JSON

Hi there,

So I’m working on a little project where I have three scenes and in each scene I have multiple buttons that when pressed I would like to load text into a single text component. So far I have looked into using json files to try this, I’m getting as far as being able to load the json as a list on to a game object. From what I’ve read it seems as though it’s better in this case to load the data into a dictionary. Where I’m getting stuck is writing a function that will allow me to check: What scene I’m in and then which button was pressed, then to load the correct bit of text.

Any help would be greatly appreciated, I’m very very early into my coding journey so don’t shoot me :smile: If anyone has any examples I’d be extremely grateful.

Is your question about JSON, or actually about scene switch?
Please narrow your problem to specific case.
Post details, of what you tried so far.

Hi @MF_Danny

“Where I’m getting stuck is writing a function that will allow me to check: What scene I’m in and then which button was pressed, then to load the correct bit of text.”

Why not just google your questions - pretty much all can be solved with first hits:

Unity get current scene

Add listener methods to buttons

Unity load data from JSON

“it’s better in this case to load the data into a dictionary.”

You can load data from JSON with the builtin JsonUtilitily, but AFAIK you can’t directly deserialize saved data to dictionary, as Unity internal serialized doesn’t support serializing dictionaries.

@eses so just googling it only gets me so far due to my limited knowledge of things. I’ve tried my best to work it out myself by doing my own research but with only a small amount of success it was time to hit the forums.

@Antypodish no, it’s not so much about json. I think I’m there with how to format it (I hope I am anyway :smile:) more about loading json, and reading through arrays to get correct parameters.

So as a bit of a further explanation, I’m fine with knowing what scene I’m in, and how to create onClick functions. I have multiple json files (so they don’t get too busy), one for each scene. In each of those files I then have an array of as many buttons I have in each scene:

{
    "SceneOne": [
        {
            "annoID": 0,
            "annoHeader": "Annotation 1",
            "annoBodyText": "This is the first annotation"
        },
        {
            "annoID": 1,
            "annoHeader": "Annotation 2",
            "annoBodyText": "This is the second annotation"
        },
        {
            "annoID": 2,
            "annoHeader": "Annotation 3",
            "annoBodyText": "This is the third annotation"
        }

    ]
}

So in this example scene one has three buttons, if button number 2 is pressed, I’d like to two text elements in the scene (Header and Body copy) to read in the correct values. I think what I’m struggling with is how load the correct index of the array, which whilst typing this makes me think the ID values are irrelevant.

@MF_Danny

Well Unity builtin JsonUtility doesn’t work quite like that - you need to have a JSON data that matches a class or hierarchy of classes…

So you definitely wouldn’t want to have “SceneOne” named JSON object, as then you would have to have a class called “SceneOne” - so did you actually read the links I gave you?

You would first need to have classes like this (explained in the docs):

// JsonUtility serializable container for list
[System.Serializable]
public class DataContainer
{
    public List<Scene> scenes;
}

// one scene
[System.Serializable]
public class Scene
{
    public int annoID;
    public string annoHeader;
    public string annoBodyText;
}

Then you can have in your code something like this:

data = JsonUtility.ToJson(container, true);

The output will look something like this (here just empty items because I created the list by adding new items to list):

{
    "scenes": [
        {
            "annoID": 0,
            "annoHeader": "",
            "annoBodyText": ""
        },
        {
            "annoID": 0,
            "annoHeader": "",
            "annoBodyText": ""
        },
        {
            "annoID": 0,
            "annoHeader": "",
            "annoBodyText": ""
        }
    ]
}

Then you can save that to file (some random example):

And later load it back:

// data from json text string:
JsonUtility.FromJsonOverwrite(data, container);

// or like this
container = JsonUtility.FromJson<DataContainer>(data);

Then you can access it like any C# class:

Debug.Log("Scene 0 id:" + container.scenes[0].annoID);
2 Likes

@eses thanks! Currently just going over the ‘quiz game’ videos you sent, as that may get me somewhere.

Many thanks for your help, I shall hopefully report back… soon…ish

@eses Many many thanks. I’ve got it working, I’m sure I’ll find issues to workout down the line but thank you very much.

1 Like