How Can I Use Json To Read Back From A File And Assign It Back To The List ?

I’m using JsonHelper but it can be also with the unity JsonUtility.
Saving it to a file using json was easy but to read back is the problem.

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

[ExecuteInEditMode]
public class DialogueTrigger : MonoBehaviour
{
    public List<Conversation> conversations = new List<Conversation>();

    [HideInInspector]
    public int dialogueNum = 0;

    [HideInInspector]
    public int dialogueIndex = 0;

    private bool triggered = false;
    private List<Dialogue> oldDialogue;
    private bool activateButton = false;

    public void TriggerDialogue(int dialogueIndex)
    {
        this.dialogueIndex = dialogueIndex;

        if (conversations.Count > 0 &&
            conversations[dialogueIndex].Dialogues.Count > 0)
        {
            if (triggered == false)
            {
                if (FindObjectOfType<DialogueManager>() != null)
                {
                    FindObjectOfType<DialogueManager>().StartDialogue(conversations[dialogueIndex].Dialogues[dialogueNum]);
                    dialogueNum += 1;
                }
                triggered = true;
            }
        }
    }

    private void Update()
    {
        ButtonActivation();

        if (DialogueManager.dialogueEnded == true)
        {
            if (dialogueNum == conversations[dialogueIndex].Dialogues.Count)
            {
                return;
            }
            else
            {
                FindObjectOfType<DialogueManager>().StartDialogue(conversations[dialogueIndex].Dialogues[dialogueNum]);
                DialogueManager.dialogueEnded = false;
                dialogueNum += 1;
            }
        }
    }

    public bool ActivateButton()
    {
        return activateButton;
    }

    private void ButtonActivation()
    {
        if (ConversationsChecks() == true)
        {
            foreach (string sentence in conversations[dialogueIndex].Dialogues[dialogueNum].sentences)
            {
                if (sentence != "")
                {
                    activateButton = true;
                }
                else
                {
                    activateButton = false;
                }
            }
        }
        else
        {
            activateButton = false;
        }
    }

    public void SaveConversations()
    {
        string jsonTransform = JsonHelper.ToJson(conversations.ToArray(), true);
    }

    public void LoadConversations()
    {
        string jsonTransform = File.ReadAllText(@"d:\json.txt");
        var t = JsonHelper.FromJson<string>(jsonTransform);
       
    }

    private bool ConversationsChecks()
    {
        bool IsConversationsReady = false;

        if (conversations.Count > 0 &&
            conversations[dialogueIndex].Dialogues.Count > 0 &&
            conversations[dialogueIndex].Dialogues[dialogueNum].sentences.Count > 0 &&
            conversations[dialogueIndex].Dialogues[dialogueNum].name != "" &&
            conversations[dialogueIndex].name != "")
        {
            IsConversationsReady = true;
        }
        else
        {
            IsConversationsReady = false;
        }

        return IsConversationsReady;
    }
}

This is the two methods SaveConversations and LoadConversations :

public void SaveConversations()
    {
        string jsonTransform = JsonHelper.ToJson(conversations.ToArray(), true);
    }

    public void LoadConversations()
    {
        string jsonTransform = File.ReadAllText(@"d:\json.txt");
        var t = JsonHelper.FromJson<string>(jsonTransform);
    }

Saving was easy but how do I read it back into the conversations List ?
For the test I’m using var t and now t is a string array string[ ]

But I need it to be assigned back into the conversations List.

Afaik, in unity json root element has to be object, not array. You need to create a struct with array,

1 Like

@dubiduboni_unit

Like @palex-nx said, wrap you data to a class:

public class SaveData
{
    public List<int> intList;
    public string stringValue;
    public float floatValue;
}

Then save it, and when you need to load it, do something like this:

void Load()
{
    var saveFilePath = Application.dataPath + "/save/data.json";  // path in project assets for testing
    var json = File.ReadAllText(saveFilePath); // read text from file
    saveData = JsonUtility.FromJson<SaveData>(json);  // assign load data
}
1 Like

I believe also that the fields in the file must match the name of the fields in the SaveData class.

1 Like