使用 SimpleJSON 读取包含数组的 JSON 个文件

Reading JSON files that contain arrays using SimpleJSON

我有一个 JSON 文件,看起来像下面的文件。 "Questions" 节点包含一个数组列表。这些数组中的每一个都包含 5 个字符串。 1个问题,4个答案。我正在尝试使用 Unity 阅读信息。内置的 JsonUtility 显然不能很好地处理嵌套 JSON 文件中的数组,所以我尝试使用 SimpleJSON 代替。它非常适合创建文件,但我不需要该功能。

我需要从我的 JSON 文件中提取所有信息。最好在像 List<List<string>>Dictionary<string,List<string>> 这样的变量中。该文件是这样创建的:

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());
    }

我已经尝试了各种不同的 foreach 循环来尝试提取数据,但到目前为止还没有奏效。这是我的尝试之一:

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);
            }
        }
    }

如果有人有从类似 Json 文件中读取数据的经验,请告诉我!我愿意使用 SimpleJSON!

以外的其他工具

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

使用 .NET 的 https://www.newtonsoft.com/json 库尝试此代码:

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
    }