im a programmer in training, so, im working with Unity, C# and i want to make NPCs with dialogues, those dialogues are stored in JSON files like this one:
{
"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."]
]
}
Im from Spain, by the way, so, i need to get specifically the "content" variable, im trying to store it in a class like this one:
//A class to store the definied conversation of the npc.
public class dialog{
public string name;
public List<string[]> content;
}
This is how i do it:
//We make an object where store the data from the dialog json.
public dialog dial;
dial = JsonUtility.FromJson<dialog>(File.ReadAllText(jsonPath));
Now, when trying to access it, the ressult is always the same error:
NullReferenceException: Object reference not set to an instance of an object
defNPC.Start () (at Assets/Scripts/NPCs/defNPC.cs:62)
And when trying to get it by console, i only get "Null".
I have tried changing the JSON, and using two classes:
{
"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;
}
But none of it seems to work, i am really really confused, is it really so hard to get a list of lists in here?
Sorry if i misspell something.
CodePudding user response:
According to https://json2csharp.com/:
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Root
{
public string name { get; set; }
public List<List<string>> content { get; set; }
}
CodePudding user response:
All right, fixed, turned ot to be that i needded to put [System.Serialize] on top of the classes, i dont know why but looks like its needed.