我有一个看起来像下面的JSON文件。 “问题”节点包含一个数组列表。这些数组中的每一个包含5个字符串。一个问题,四个答案。我正在尝试使用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之外的其他工具!
{
"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"
]
}
}
答案 0 :(得分:1)
使用{.3} .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
}