简体   繁体   中英

"NullReferenceException" in Unity3D when trying deserialize data from JSON

I need to get some data from json file i've got it to the string named dataAsJson, on debugging it looks like that:

    {\r\n\t\"question\":\r\n\t[\r\n\t\t{\r\n\t\t\"text\": 
    \"głośniki\",\r\n\t\t\"correct\": \"speaker\",\r\n\t\t\"answer1\": 
    \"speaker1\",\r\n\t\t\"answer2\": \"speaker2\",\r\n\t\t\"answer3\": 
    \"speaker3\"\r\n\t\t},\r\n\t\t{\r\n\t\t\"text\": 
    \"pustynia\",\r\n\t\t\"correct\": \"desert\",\r\n\t\t\"answer1\": 
    \"desert1\",\r\n\t\t\"answer2\": \"desert2\",\r\n\t\t\"answer3\": 
    \"deser3\"\r\n\t\t},\r\n\t\t{\r\n\t\t\"text\": 
    \"rycerz\",\r\n\t\t\"correct\": \"knight\",\r\n\t\t\"answer1\": 
    \"knight1\",\r\n\t\t\"answer2\": \"knight2\",\r\n\t\t\"answer3\": 
    \"knight3\"\r\n\t\t}\r\n\t]\r\n}

And then i use JsonUtility.FromJson(dataAsjJson) This is my objects classes:

    public class Question
    {
     public string text { get; set; }
     public string correct { get; set; }
     public string answer1 { get; set; }
     public string answer2 { get; set; }
     public string answer3 { get; set; }
    }
    public class RootObject
    {
        public List<Question> questions { get; set; }
    }

As you can see the json body is only the "question" with array. On Debug.log(dataAsJson) it looks normally:

    {
        "question":
        [
            {
            "text": "głośniki",
            "correct": "speaker",
            "answer1": "speaker1",
            "answer2": "speaker2",
            "answer3": "speaker3"
            },
            {
            "text": "pustynia",
            "correct": "desert",
            "answer1": "desert1",
            "answer2": "desert2",
            "answer3": "deser3"
            },
            {
            "text": "rycerz",
            "correct": "knight",
            "answer1": "knight1",
            "answer2": "knight2",
            "answer3": "knight3"
            }
        ]
    }

I need to convert it succesfully to C# object.

As mentioned by Amy in the comments, your C# class structure doesn't match your JSON, I use http://json2csharp.com/ because it's accurate and quick. Make sure you put [Serializable] above each class so we can transform data structures or object states into a format that Unity can store and reconstruct later read more here . Lastly, I would avoid getters and setters in this structure, it will cause problems with serializing & is recommended by unity to use fields instead.

[Serializable]
public class Question
{
    public string text;
    public string correct;
    public string answer1;
    public string answer2;
    public string answer3;
}
[Serializable]
public class RootObject
{
    public List<Question> question;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM