0

I have the following serialized class:

[System.Serializable]
public class Question
{
    public string question;
    public List<string> answers;
    public string correct;

    public Question(string question, List<string> answers, string correct){
        this.question = question;
        this.answers = answers;
        this.correct = correct;
    }
}

And the following array of questions:

questions = new List<GameManager.Question>();
questions.Add(new GameManager.Question("2+2?", new List<string> { "4", "5", "6" }, "4"));
questions.Add(new GameManager.Question("2*2?", new List<string> { "1", "4", "8" }, "4"));
questions.Add(new GameManager.Question("2/2?", new List<string> { "0", "1", "2" }, "1"));

I need to store this information as jsonformat and load it to init the question game.

How I can load an array of questions using json?

Lechucico
  • 1,914
  • 7
  • 27
  • 60
  • You can use `NewtonSoft.JsonConvert.SerializeObject` and `Newtonsoft.JsonConvert.DeserializeObject` if you have that nuget package installed. Then just decorate your properties with a `JsonProperty` tag, documentation (https://www.newtonsoft.com/json/help/html/SerializingJSON.htm) – Ryan Wilson Oct 10 '18 at 20:02
  • 1
    Have you tried looking online for C# libraries that allows you to work with JSON? If you haven't, you should do so before posing a question on Stack Overflow. If you have, you should explain what you've tried already, and why it hasn't met your requirements. – Serlite Oct 10 '18 at 20:04
  • JsonUtility.ToJson(object) will convert your Question into a string in json format. – Everts Oct 10 '18 at 20:28

3 Answers3

1

You can use newtonsoft.json to serialize and deserialize the objects. It is very simple to use. I think you have to install the nuget package.

Here is a post on how to serialize and save data in unity 3d: Post

juliushuck
  • 1,398
  • 1
  • 11
  • 25
1

Here's a working example

void Main()
{
    var questions = new List<Question>();
    questions.Add(new Question("2+2?", new List<string> { "4", "5", "6" }, "4"));
    questions.Add(new Question("2*2?", new List<string> { "1", "4", "8" }, "4"));
    questions.Add(new Question("2/2?", new List<string> { "0", "1", "2" }, "1"));

    var json = JsonConvert.SerializeObject(questions, Newtonsoft.Json.Formatting.Indented);
    Console.WriteLine(json);
}

// Define other methods and classes here
public class Question
{
    public string question;
    public List<string> answers;
    public string correct;

    public Question(string question, List<string> answers, string correct)
    {
        this.question = question;
        this.answers = answers;
        this.correct = correct;
    }
}

Output

[
  {
    "question": "2+2?",
    "answers": [
      "4",
      "5",
      "6"
    ],
    "correct": "4"
  },
  {
    "question": "2*2?",
    "answers": [
      "1",
      "4",
      "8"
    ],
    "correct": "4"
  },
  {
    "question": "2/2?",
    "answers": [
      "0",
      "1",
      "2"
    ],
    "correct": "1"
  }
]

You can deserialize it back to a collection of questions by

var deserialized = JsonConvert.DeserializeObject<Question[]>(json);

enter image description here

Aydin
  • 15,016
  • 4
  • 32
  • 42
0

You can use NewtonSoft.Json.JsonConvert.SerializeObject and Newtonsoft.Json.JsonConvert.DeserializeObject<T> if you have that nuget package installed. Then just decorate your properties with a JsonProperty tag, documentation (https://www.newtonsoft.com/json/help/html/SerializingJSON.htm)

Ryan Wilson
  • 10,223
  • 2
  • 21
  • 40