1

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.

0

2 Answers 2

3

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; }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

It keeps telling me that JsonConvert its not declared.
I am not interested on using jsonNET, or however is written, thank you anyway.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.