C#,Unity3D,JSON解析:无法将JSON解析为C#对象

时间:2017-09-25 15:08:14

标签: c# json

我一直在使用统一引擎构建英语游戏。我正在努力让C#解析JSON。

过去几天我一直试图让JSON解析,但没有成功。我已经验证了JSON,可以将jsonString调试到控制台,但只要jsonUtility在json上得到它,那么我最终会加载一些NULL数据。

这是我的json:

 {"question": {"questionType": "0", "categoryID": "0", "bonusBool": "FALSE", "answerText": "cake", "questionText": "If something is really easy to do, it\u2019s a piece of \u2026\u2026\u2026 \\n a. pie \\n b. tart \\n c. cake", "ID": "0"}}

这是我的C#:

 [System.Serializable]
 public class QuestionDataAsString
 {

     public string questionText;
     public string answerText;
     public string categoryID;
     public string ID;
     public string questionType;
     public string bonusBool;

 }

 [System.Serializable]
 public class Question{

     public string questionText;
     public string answerText;
     public int categoryID;
     public int ID;
     public int questionType;
     public bool bonusBool;

 }

 public static void LoadQuestion()
     {
         string filePath = Path.Combine(Application.streamingAssetsPath, "questionData.json");
         string dataAsJSON = File.ReadAllText(filePath);
         Debug.Log(dataAsJSON);
         QuestionDataAsString question = JsonUtility.FromJson<QuestionDataAsString>(dataAsJSON);
         Debug.Log(question.questionText);
     }

 public static Question ConvertQuestionDataFromString(QuestionDataAsString stringData)
     {
         Question question = new Question();
         question.questionText = stringData.questionText;
         question.answerText = stringData.answerText;
         question.categoryID = int.Parse(stringData.categoryID);
         question.ID = int.Parse(stringData.ID);
         question.questionType = int.Parse(stringData.questionType);
         question.bonusBool = Boolean.Parse(stringData.bonusBool);
         return question;
     }

当我通过启动画面控制器运行LoadQuestion时,我可以将json作为字符串记录到控制台。但是一旦我使用了jsonUtility,我就会得到Null对象。

这是控制台输出:

 {"question": {"questionType": "0", "categoryID": "0", "bonusBool": "FALSE", "answerText": "cake", "questionText": "If something is really easy to do, it\u2019s a piece of \u2026\u2026\u2026 \\n a. pie \\n b. tart \\n c. cake", "ID": "0"}}
 UnityEngine.Debug:Log(Object)
 SaveLoadManager:LoadQuestion() (at Assets/Scripts/SaveLoadManager.cs:91)
 <Start>c__Iterator0:MoveNext() (at Assets/Scripts/SplashController.cs:18)
 UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)

 Null
 UnityEngine.Debug:Log(Object)
 SaveLoadManager:LoadQuestion() (at Assets/Scripts/SaveLoadManager.cs:93)
 <Start>c__Iterator0:MoveNext() (at Assets/Scripts/SplashController.cs:18)
 UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)

我在这里和一致的答案中搜索了答案,但没有找到任何相关的解决方案。

为什么来自Json的任何想法都会为所有事情返回Null?

1 个答案:

答案 0 :(得分:3)

您的JSON与您的C#类结构不匹配。您需要一个根级别对象来保存问题详细信息。此外,您应该使用属性,而不是字段。例如:

public class QuestionRoot
{
    public Question Question { get; set; }
}

public class Question
{
    public string questionType { get; set; }
    public string categoryID { get; set; }
    public string bonusBool { get; set; }
    public string answerText { get; set; }
    public string questionText { get; set; }
    public string ID { get; set; }
}

现在你可以这样做:

var questionRoot = JsonUtility.FromJson<QuestionRoot>(dataAsJSON);
var questionText = questionRoot.question.questionText;

旁注:您应该考虑遵循C#常用做法进行命名。属性几乎总是以大写字母开头:

public string QuestionText { get; set; }