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);
        }
    }
}
KeyvanSFX
  • 113
  • 1
  • 11
LearnProgramming
  • 814
  • 1
  • 12
  • 37

1 Answers1

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

IShix
  • 186
  • 7
  • it output the string json, but when i try to debug using for loop, i facing this problem `QuizLists' does not contain a definition for 'Count' and no accessible extension method 'Count' accepting a first argument of type 'QuizLists' could be found (are you missing a using directive or an assembly reference?)` – LearnProgramming Dec 07 '22 at 08:13
  • 1
    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 – Everts Dec 07 '22 at 08:49
  • btw, is it possible to output json string for question_list too? – LearnProgramming Dec 07 '22 at 10:44
  • 1
    This can be done by wrapping the `List` with a `class` as shown above. – IShix Dec 07 '22 at 10:49
  • 1
    you can reuse the same concept or make the QuizLists as a public class JsonList{ public List 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. – Everts Dec 07 '22 at 10:50
  • 1
    @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. – IShix Dec 07 '22 at 10:57
  • 1
    @LearnProgramming If you want to use Json seriously, `JsonUtility` is certainly not enough. It is better to use an external JSON serialization framework. – IShix Dec 07 '22 at 11:02
  • 1
    @LearnProgramming Please take a look at the `ScriptableObject` sample I added to the answer. – IShix Dec 07 '22 at 11:15
  • @Yuta thank you very much, today i learned something new! will definitely use ScriptableObject – LearnProgramming Dec 07 '22 at 16:13