【问题标题】:Parse JSON into an array of objects [duplicate]将 JSON 解析为对象数组 [重复]
【发布时间】: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.SerializeObjectNewtonsoft.JsonConvert.DeserializeObject&lt;T&gt;。然后用JsonProperty 标签、文档(newtonsoft.com/json/help/html/SerializingJSON.htm)装饰你的属性
  • 您是否尝试过在线查找允许您使用 JSON 的 C# 库?如果您还没有,您应该在 Stack Overflow 上提出问题之前这样做。如果你有,你应该解释你已经尝试过什么,以及为什么它没有满足你的要求。
  • JsonUtility.ToJson(object) 会将您的问题转换为 json 格式的字符串。

标签: c# json unity3d


【解决方案1】:

您可以使用 newtonsoft.json 来序列化和反序列化对象。使用起来非常简单。我认为你必须安装 nuget 包。

这是一篇关于如何在 unity 3d 中序列化和保存数据的帖子:Post

【讨论】:

    【解决方案2】:

    这是一个工作示例

    void Main()
    {
        var questions = new List<Question>();
        questions.Add(new Question("2+2?", new List<string> { "4", "5", "6" }, "4"));
        questions.Add(new Question("2*2?", new List<string> { "1", "4", "8" }, "4"));
        questions.Add(new Question("2/2?", new List<string> { "0", "1", "2" }, "1"));
    
        var json = JsonConvert.SerializeObject(questions, Newtonsoft.Json.Formatting.Indented);
        Console.WriteLine(json);
    }
    
    // Define other methods and classes here
    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;
        }
    }
    

    输出

    [
      {
        "question": "2+2?",
        "answers": [
          "4",
          "5",
          "6"
        ],
        "correct": "4"
      },
      {
        "question": "2*2?",
        "answers": [
          "1",
          "4",
          "8"
        ],
        "correct": "4"
      },
      {
        "question": "2/2?",
        "answers": [
          "0",
          "1",
          "2"
        ],
        "correct": "1"
      }
    ]
    

    您可以通过

    将其反序列化回问题集合
    var deserialized = JsonConvert.DeserializeObject<Question[]>(json);
    

    【讨论】:

    • 我需要从 json 文件生成 Question 数组,而不是从该数组生成 json 文件。
    • @Lechucico 然后你使用Newtonsoft.Json.DeserializeObject&lt;T&gt;,其中T 是List&lt;Question&gt;,请参阅我的回答或评论以获取文档。
    • 更新@Lechucico
    • 感谢@AydinAdn。关于如何在 Unity 上导入 JsonConvert 包的任何想法?
    【解决方案3】:

    如果您安装了nuget package,则可以使用NewtonSoft.Json.JsonConvert.SerializeObjectNewtonsoft.Json.JsonConvert.DeserializeObject&lt;T&gt;。然后用JsonProperty 标签、文档(https://www.newtonsoft.com/json/help/html/SerializingJSON.htm)装饰你的属性

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 2020-05-02
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      相关资源
      最近更新 更多