【发布时间】:2018-10-10 19:59:24
【问题描述】:
我有以下序列化类:
[System.Serializable]
public class Question
{
public string question;
public List<string> answers;
public string correct;
public Question(string question, List<string> answers, string correct){
this.question = question;
this.answers = answers;
this.correct = correct;
}
}
还有下面的questions数组:
questions = new List<GameManager.Question>();
questions.Add(new GameManager.Question("2+2?", new List<string> { "4", "5", "6" }, "4"));
questions.Add(new GameManager.Question("2*2?", new List<string> { "1", "4", "8" }, "4"));
questions.Add(new GameManager.Question("2/2?", new List<string> { "0", "1", "2" }, "1"));
我需要将此信息存储为jsonformat 并加载它以启动问题游戏。
如何使用 json 加载一系列问题?
【问题讨论】:
-
如果您安装了该 nuget 包,您可以使用
NewtonSoft.JsonConvert.SerializeObject和Newtonsoft.JsonConvert.DeserializeObject<T>。然后用JsonProperty标签、文档(newtonsoft.com/json/help/html/SerializingJSON.htm)装饰你的属性 -
您是否尝试过在线查找允许您使用 JSON 的 C# 库?如果您还没有,您应该在 Stack Overflow 上提出问题之前这样做。如果你有,你应该解释你已经尝试过什么,以及为什么它没有满足你的要求。
-
JsonUtility.ToJson(object) 会将您的问题转换为 json 格式的字符串。