0

I have script as follows but when i try to make json file, it just return empty {}

QuizData.cs

using System.Collections.Generic;

[System.Serializable]
public class QuizList
{
    public string quizName;
    public List<QuestionList> questionList = new List<QuestionList>();

    public QuizList(string quizName, List<QuestionList> questionList)
    {
        this.quizName = quizName;
        this.questionList = questionList;
    }
}

[System.Serializable]
public class QuestionList
{
    public string question;
    public List<string> choice = new List<string>();
    public int option;

    public QuestionList(string question, List<string> choice, int option)
    {
        this.question = question;
        this.choice = choice;
        this.option = option;
    }
}

datatest.cs

public class datatest : MonoBehaviour
{
    List<QuizList> quiz_list = new List<QuizList>();
    List<QuestionList> question_list = new List<QuestionList>();
    List<string> choices = new List<string>();

    void Awake()
    {
        choices.Add("Aaa");
        choices.Add("Bbb");
        choices.Add("Ccc");

        question_list.Add(new QuestionList(
            "I'm Gonna Be (500 Miles)",
            choices,
            1
        ));

        quiz_list.Add(new QuizList(
            "sadasdasd",
            question_list
        ));

        var json = JsonUtility.ToJson(quiz_list);
        Debug.Log(json); // this return empty json string `{}`

    }
}

debug output here

// Debug output
for (int i = 0; i < quiz_list.Count; i++)
{
    Debug.Log(quiz_list[i].quizName);

    foreach (var ql in quiz_list[i].questionList)
    {
        Debug.Log(ql.question);
        Debug.Log(ql.question);

        foreach (var c in ql.choice)
        {
            Debug.Log(c);
        }
    }
}

1 Answer 1

1

In addition, the List must be wrapped in a class.


// Add wrapper class for List<QuizList>
[System.Serializable]
public class QuizLists
{
    public List<QuizList> lists;

    public QuizLists(List<QuizList> lists)
    {
        this.lists = lists;
    }
}

// test

using System.Collections.Generic;
using UnityEngine;

public class datatest : MonoBehaviour
{
    QuizLists quiz_list;
    List<QuestionList> question_list = new List<QuestionList>();
    List<string> choices = new List<string>();

    void Awake()
    {
        choices.Add("Aaa");
        choices.Add("Bbb");
        choices.Add("Ccc");

        question_list.Add(new QuestionList(
            "I'm Gonna Be (500 Miles)",
            choices,
            1
        ));

        quiz_list = new QuizLists(new List<QuizList>()
        {
            new QuizList(
                "sadasdasd",
                question_list
            )
        });

        var json = JsonUtility.ToJson(quiz_list);
        Debug.Log(json);

    }
}

BTW: If you are dealing with master data, ScriptableObject is recommended because it is faster.

QuizListsMasterData.cs

using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(fileName = "QuizListsMasterData", menuName = "ScriptableObject/QuizListsMasterData")]
public sealed class QuizListsMasterData : ScriptableObject
{
    public List<QuizList> lists;
}

Generate from right-click on Project.

enter image description here

enter image description here

Sign up to request clarification or add additional context in comments.

9 Comments

QuizLists is a wrapping class for the lists because JsonUtility does not support collection as main object in json. QuizLists is not the list, the list is at quiz_list.lists.Count
This can be done by wrapping the List with a class as shown above.
you can reuse the same concept or make the QuizLists as a public class JsonList<T>{ public List<T> lists; } Though I wonder if JsonUtility support generic... one other way is to use another json library like JsonFx or NewtonSoft. Those have wider support.
@LearnProgramming BTW: The data we are dealing with this time feels like static data, like master data. In that case, ScriptableObject is recommended because it is faster. Please consider it.
@LearnProgramming If you want to use Json seriously, JsonUtility is certainly not enough. It is better to use an external JSON serialization framework.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.