提问人:Martin Jose Morata Garcia 提问时间:6/7/2022 更新时间:6/9/2022 访问量:59
我在从 JSON 文件存储数组时遇到问题
I have problems storing an array of arrays from a JSON file
问:
我是一名正在接受培训的程序员,所以,我正在使用 Unity、C#,我想制作带有对话的 NPC,这些对话存储在 JSON 文件中,如下所示:
{
"name":"Test sign",
"content":[
["Hay algo escrito en este cartel..."],
["Pulsa el botón adecuado para leer este cartel."],
["Acavas de leer lo que pone en el cartel."],
["Te relajas al saber lo que pone en el cartel."],
["Nunca sabrás lo que pone en este cartel."]
]
}
顺便说一下,我来自西班牙,所以,我需要专门获取“内容”变量,我试图将其存储在这样的类中:
//A class to store the definied conversation of the npc.
public class dialog{
public string name;
public List<string[]> content;
}
我是这样做的:
//We make an object where store the data from the dialog json.
public dialog dial;
dial = JsonUtility.FromJson<dialog>(File.ReadAllText(jsonPath));
现在,当尝试访问它时,ressult 始终是相同的错误:
NullReferenceException: Object reference not set to an instance of an object
defNPC.Start () (at Assets/Scripts/NPCs/defNPC.cs:62)
当尝试通过控制台获取它时,我只得到“Null”。
我尝试更改 JSON,并使用两个类:
{
"name":"Test sign",
"content":[
{"list":["Hay algo escrito en este cartel..."]},
{"list":["Pulsa el botón adecuado para leer este cartel."]},
{"list":["Acavas de leer lo que pone en el cartel."]},
{"list":["Te relajas al saber lo que pone en el cartel."]},
{"list":["Nunca sabrás lo que pone en este cartel."]}
]
}
public class SingleArr{
public string[] list;
}
//A class to store the definied conversation of the npc.
public class dialog{
public string name;
public SingleArr[] content;
}
但是这些似乎都不起作用,我真的很困惑,在这里获得列表列表真的那么难吗?
对不起,如果我拼错了什么。
答:
3赞
Robert Harvey
6/7/2022
#1
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Root
{
public string name { get; set; }
public List<List<string>> content { get; set; }
}
评论
0赞
Martin Jose Morata Garcia
6/8/2022
它一直告诉我 JsonConvert 它没有声明。
0赞
Martin Jose Morata Garcia
6/8/2022
我对使用 jsonNET 不感兴趣,或者无论如何编写,无论如何都谢谢。
0赞
Martin Jose Morata Garcia
6/9/2022
#2
好吧,已修复,原来我需要将 [System.Serialize] 放在类之上,我不知道为什么,但看起来需要它。
评论