유니티 엔진을 사용하여 영어 게임을 제작했습니다. 나는 C#에서 JSON을 파싱하는 데 어려움을 겪고있다.C#, Unity3D, JSON 구문 분석 : JSON을 C# 개체로 구문 분석하지 못했습니다.
지난 며칠 동안 JSON을 구문 분석하려고했지만 실패했습니다. JSON의 유효성을 검사하고 콘솔에 jsonString을 디버깅 할 수 있지만 jsonUtility가 json에 손을 넣 자마자 NULL 데이터로드로 끝납니다. 나는되는 SplashScreen 컨트롤러를 통해 LoadQuestion을 실행하면
[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;
}
, 내가 문자열로 콘솔에 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#입니다 :
여기 내 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)
내가 여기에 대한 답변을 통해 화합의 답변에 검색했지만, 모든 관련 솔루션을 발견하지 않았습니다.
fromJson이 Null을 모든 것으로 반환하는 이유는 무엇입니까?
JSON이 C# 클래스 구조와 일치하지 않습니다. – DavidG
JSON 클래스를 사용하여 행운을 빌어 요, 화합하지 않는 것 JSON 아래에서 중첩 된 레벨 –
@ johnny5 무엇이 문제입니까? 이것은 단지 2 단계입니다. – DavidG