【问题标题】:Unity JsonUtility.FromJson returns nullUnity JsonUtility.FromJson 返回 null
【发布时间】:2022-07-20 06:02:02
【问题描述】:

我正在尝试使用 JsonUtility.FromJson 将 json 文件转换为 c# 类,但它返回 null。

这是json文件

{
"questions": [
    {
        "questionName": "What is the color of the sun?",
        "wrongAnswers": [
            "Red",
            "Blue",
            "Green"
        ],
        "trueAnswer": "Yellow"
    },
    {
        "questionName": "What is the job of a person who works at a hospital?",
        "wrongAnswers": [
            "Cook",
            "Driver",
            "Teacher"
        ],
        "trueAnswer": "Doctor"
    },
    {
        "questionName": "How do you write the number 19?",
        "wrongAnswers": [
            "Nine",
            "Eighteen",
            "Eleven"
        ],
        "trueAnswer": "Nineteen"
    },
    {
        "questionName": "What is 14+17",
        "wrongAnswers": [
            "Nineteen",
            "Twentyone",
            "Fortyfive"
        ],
        "trueAnswer": "Thirtyone"
    },
    {
        "questionName": "What is the color of the sky?",
        "wrongAnswers": [
            "Red",
            "Blue",
            "Green"
        ],
        "trueAnswer": "Blue"
    },
    {
        "questionName": "What is the color of the sea?",
        "wrongAnswers": [
            "Red",
            "Yellow",
            "Green"
        ],
        "trueAnswer": "Blue"
    },
    {
        "questionName": "Rabits can ___.",
        "wrongAnswers": [
            "Fly",
            "Talk",
            "Write"
        ],
        "trueAnswer": "Run"
    },
    {
        "questionName": "I like _____ a book.",
        "wrongAnswers": [
            "swimming",
            "walking",
            "riding"
        ],
        "trueAnswer": "reading"
    },
    {
        "questionName": "What is the insect with 8 legs?",
        "wrongAnswers": [
            "Ladybug",
            "Ant",
            "Fly"
        ],
        "trueAnswer": "Spider"
    },
    {
        "questionName": "What is the nationality of a person from India",
        "wrongAnswers": [
            "Indi",
            "Inda",
            "Indain"
        ],
        "trueAnswer": "Indian"
    },
    {
        "questionName": "How many legs does a cat have?",
        "wrongAnswers": [
            "Three",
            "Six",
            "Eight"
        ],
        "trueAnswer": "Four"
    },
    {
        "questionName": "A teacher works at ______.",
        "wrongAnswers": [
            "Office",
            "Hospital",
            "Restaurant"
        ],
        "trueAnswer": "School"
    },
    {
        "questionName": "I live ___ Istanbul.",
        "wrongAnswers": [
            "with",
            "at",
            "on"
        ],
        "trueAnswer": "in"
    },
    {
        "questionName": "_____ is the color of milk",
        "wrongAnswers": [
            "Blue",
            "Orange",
            "Red"
        ],
        "trueAnswer": "White"
    },
    {
        "questionName": "A ____ lives in the forest",
        "wrongAnswers": [
            "Cat",
            "Dog",
            "Mouse"
        ],
        "trueAnswer": "Monkey"
    },
    {
        "questionName": "It's half past ten. An hour passes. What time it is now?",
        "wrongAnswers": [
            "10.40",
            "9.30",
            "11.20"
        ],
        "trueAnswer": "11.30"
    }
]
}

这里是c#文件

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

[System.Serializable]
public class Question
{
    public string questionName { get; set; }
    public string[] wrongAnswers { get; set; }
    public string trueAnswer { get; set; }
}

[System.Serializable]
public class Questions
{
    public Question[] questions { get; set; }
}

public class QuestionSetter : MonoBehaviour
{
private bool isActivated = true;
public TextAsset questionsJsonFile;
public Text[] answerTexts;
public Text questionTextObject;
private Questions questionInJson;

void Start(){
    questionInJson = JsonUtility.FromJson<Questions>(questionsJsonFile.text);
}

void Update(){
    Debug.Log(questionInJson.questions);
    if(gameObject.activeSelf && isActivated){
        int randomValue = Random.Range(0, 16);
        int random2 = Random.Range(0, 3);
        int i = 0;
        int b = 0;
        int s = 0;
        for(i = 0; i < 16; i++){
            if(i == randomValue){
                Debug.Log(questionsJsonFile.text);
                questionTextObject.text = questionInJson.questions[i].questionName;
                foreach(Text answer in answerTexts){
                    if(b == random2){
                        answer.text = questionInJson.questions[i].trueAnswer;
                    }else{
                        answer.text = questionInJson.questions[i].wrongAnswers[s];
                        s++;
                    }
                    b++;
                }
            }
        }
    }
}
}

当我尝试 Debug.Log(questionsInJson) 时,它说它是一个 Questions 对象,但是当我尝试 Debug.Log(questionsInJson.questions) 时,它说它为空。有谁知道为什么它为空?请帮忙。

【问题讨论】:

    标签: c# json unity3d


    【解决方案1】:

    Unity 串行器doesn't built-in support properties ({get; set; })!

    改用fields(=> 删除所有{get; set; })!

    [Serializable]
    public class Question
    {
        public string questionName;
        public string[] wrongAnswers;
        public string trueAnswer;
    }
    
    [Serializable]
    public class Questions
    {
        public Question[] questions;
    }
    

    或者——虽然我在你的用例中看不出任何理由——你可以通过另外添加属性[field: SerializeField]来强制属性序列化

    [Serializable]
    public class Question
    {
        [field: SerializeField] public string questionName {get; set;}
        [field: SerializeField] public string[] wrongAnswers {get; set;}
        [field: SerializeField] public string trueAnswer {get; set;}
    }
    
    [Serializable]
    public class Questions
    {
        [field: SerializeField] public Question[] questions {get; set;}
    }
    

    【讨论】:

    • 感谢您的回答。
    猜你喜欢
    • 1970-01-01
    • 2015-12-18
    • 1970-01-01
    • 2017-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-03
    相关资源
    最近更新 更多