1

I'm trying to create a custom revision quiz program in Unity C# which allows users to load questions into the program using JSON files, structured as below:

{
  "question": [
    {
      "title": "What wave characteristic is measured on the vertical axis?",
      "answers": {
        "correct": "Amplitude",
        "wrong": [
          "Frequency",
          "Period",
          "Speed"
        ]
      }
    },
    {
      "title": "Which of these is a vector quantity?",
      "answers": {
        "correct": "Velocity",
        "wrong": [
          "Speed",
          "Time",
          "Mass"
        ]
      }
    }
  ]
}

I've managed to get my program reading from a file using a StreamReader, but am having a lot of trouble trying to get it into a single data structure.

I have seen other solutions using classes and manually defining structures for their solutions, but I don't know how to go about implementing this for a) as complex a structure as this and b) a structure that can have an arbritrary number of items in it (I'd like to support any number of questions). If the best way is to define these classes, how do I go about referencing items inside them? In the past I've parsed JSON using Python 3.6's json library's json.loads() function, and that worked perfectly, creating a single multidimensional array / dictionary structure that I could work with easily.

To put it simply, I currently have a string that I've read from a file with JSON data in it. How do I synthesise this into a single array that I can easily access using, eg, questions[question][0]["title"], which would return "What wave characteristic is measured on the vertical axis?" in the above case?

  • Your Json structure suggests that there is ever only 1 question. You can create a set of classes which would match your Json model and deserialize the Json into objects of those classes. Check out https://www.newtonsoft.com/json/help/html/DeserializeObject.htm – Vidmantas Blazevicius Feb 04 '18 at 16:01
  • http://json2csharp.com/ – L.B Feb 04 '18 at 16:05

2 Answers2

1

Use this site and generate your model.

    public class Answers
{
    public string correct { get; set; }
    public List<string> wrong { get; set; }
}

public class Question
{
    public string title { get; set; }
    public Answers answers { get; set; }
}

public class RootObject
{
    public List<Question> question { get; set; }
}

var model = JsonConvert.DeserializeObject<RootObject>(jsonstring);

That is all

BTW: You can also access to those properties dynamically without declaring any model

var model = JObject.Parse(jsonstring);
var title0 = (string)model["question"][0]["title"];

PS: I used Json.net

Eser
  • 12,346
  • 1
  • 22
  • 32
0

I think one way you can create a single data structure in your application by using the JSON given above is using the "paste special feature" of visual studio.Once you select it from the edit menu you have option to create class from JSON by just pasting any valid JSON. enter image description here

I get the following class after pasting your JSON related to Questions:-

public class Rootobject
{
public Question[] question { get; set; }
}

public class Question
{
public string title { get; set; }
public Answers answers { get; set; }
}

public class Answers
{
public string correct { get; set; }
public string[] wrong { get; set; }
}

Single Rootobject class consists of the Question array.There different classes automatically created by visual studio related to Question and Answers.

You can deserialize the JSON values into your RootObject using JSON.NET desrialization:

var questions= JsonConvert.DeserializeObject<RootObject>(jsonString);
ashish
  • 2,028
  • 1
  • 10
  • 6