Asgeir
Asgeir

Reputation: 677

Reading JSON files that contain arrays using SimpleJSON

I have a JSON file that looks like the one beneath. The "Questions" node contains a list of arrays. Each one of these arrays contains 5 strings. One questions, 4 answers. I'm trying to read the information using Unity. The built in JsonUtility apparently can't handle arrays in nested JSON files very well, so I tried using SimpleJSON instead. It works great for creating a file, however I do not need that functionality.

I need to extract all the information from my JSON file. Preferably in a variable like List<List<string>>, or Dictionary<string,List<string>>. The file was created like so:

public void SerializeData()
    {
        JSONObject questionJson1 = new JSONObject();
        JSONObject questionJson = new JSONObject();

        for (int i = 0; i < 5; i++)
        {
            JSONArray question = new JSONArray();
            question.Add("This is the question string itself " + i);
            question.Add("Correct answer! " + i);
            question.Add("Answer 1 " + i);
            question.Add("Answer 2 " + i);
            question.Add("Answer 3 " + i);
            questionJson1.Add(i+"", question);
        }

        questionJson.Add("Questions",questionJson1);

        File.WriteAllText(path, questionJson.ToString());
    }

I have tried all sorts of different foreach loops to try and extract the data, but it has not worked so far. Here is one of my attemps:

public void DeserializeData()
    {
        string jsonString = File.ReadAllText(path);
        JSONObject questions = (JSONObject)JSON.Parse(jsonString);
        print("UNDER HERE");
        foreach(JSONArray s in questions.AsArray[0])
        {
            foreach(KeyValuePair<string, SimpleJSON.JSONNode> t in s)
            {
                print(t.Key + " " + t.Value);
            }
        }
    }

If anyone has any experience with reading data from similar Json files, please let me know! I'm open to using other tools than SimpleJSON!

{
"Questions":{
"0":[
"This is the question string itself 0", "Correct answer! 0", "Answer 1 0", "Answer 2 0", "Answer 3 0" ], "1":[
"This is the question string itself 1", "Correct answer! 1", "Answer 1 1", "Answer 2 1", "Answer 3 1" ], "2":[
"This is the question string itself 2", "Correct answer! 2", "Answer 1 2", "Answer 2 2", "Answer 3 2" ], "3":[
"This is the question string itself 3", "Correct answer! 3", "Answer 1 3", "Answer 2 3", "Answer 3 3" ], "4":[
"This is the question string itself 4", "Correct answer! 4", "Answer 1 4", "Answer 2 4", "Answer 3 4" ] } }

Upvotes: 0

Views: 820

Answers (1)

Rahul Sharma
Rahul Sharma

Reputation: 8311

Try this code using the https://www.newtonsoft.com/json library for .NET:

using Newtonsoft.Json.Linq;

string json =
   "{\r\n\r\n   \"Questions\":{\r\n\r\n      \"0\":[\r\n\r\n         \"This is the question string itself 0\",\r\n         \"Correct answer! 0\",\r\n         \"Answer 1 0\",\r\n         \"Answer 2 0\",\r\n         \"Answer 3 0\"\r\n      ],\r\n      \"1\":[\r\n\r\n         \"This is the question string itself 1\",\r\n         \"Correct answer! 1\",\r\n         \"Answer 1 1\",\r\n         \"Answer 2 1\",\r\n         \"Answer 3 1 \"\r\n      ],\r\n      \"2\":[\r\n\r\n         \"This is the question string itself 2\",\r\n         \"Correct answer! 2\",\r\n         \"Answer 1 2\",\r\n         \"Answer 2 2\",\r\n         \"Answer 3 2 \"\r\n      ],\r\n      \"3\":[\r\n\r\n         \"This is the question string itself 1\",\r\n         \"Correct answer! 1\",\r\n         \"Answer 1 3\",\r\n         \"Answer 2 3\",\r\n         \"Answer 3 3\"\r\n      ],\r\n      \"4\":[\r\n\r\n         \"This is the question string itself 1\",\r\n         \"Correct answer! 1\",\r\n         \"Answer 1 1\",\r\n         \"Answer 2 1\",\r\n         \"Answer 3 4\"\r\n      ]\r\n    }\r\n}";

    JObject jo = JObject.Parse(json);
    Dictionary<string, List<string>> values = jo.SelectToken("Questions", false).ToObject<Dictionary<string, List<string>>>();

    //This will run 5 times as per your JSON structure
    foreach (var kv in values)
    {
        Console.WriteLine(kv.Value[0]); //Your question
        Console.WriteLine(kv.Value[1]); //Correct Answer
        Console.WriteLine(kv.Value[2]); //Answer 1
        Console.WriteLine(kv.Value[3]); //Answer 2
        Console.WriteLine(kv.Value[4]); //Answer 3
    }

Upvotes: 2

Related Questions