简体   繁体   English

如何将 Json 文件(包含数组)转换为列表(类对象的)C# Unity

[英]How do I convert a Json file (that contains a array) to a List (of a class object) C# Unity

i'm using a NewtonSoft Json API to read my dialogue texts and i'm having problem to populate a List of dialogues information.我正在使用 NewtonSoft Json API 来阅读我的对话文本,但我在填充对话信息列表时遇到了问题。

i would like to populate my list (of this class below) using one of this 2 Json formats.我想使用这 2 种 Json 格式之一来填充我的列表(下面这个类)。

[System.Serializable]
public class DialogueList
{
    public string dialogueName;
    public bool isDialogueOption;
    public string[] dialogueText;

    public string option1;
    public string option2;
}

Ex: I would like if the list be like this.例如:我希望列表是这样的。

//(Slot 1)
dialogueList[0].dialogueName = "Nyma";
dialogueList[0].isDialogueOption = true;
dialogueList[0].dialogueText[0] = "Hi Xire! how are you?";
dialogueList[0].dialogueText[1] = "Hi Nyma! i'm fine and you?";

dialogueList[0].option1 = "Fine!";
dialogueList[0].option2 = "I'm not fine!";

//(Slot2)
dialogueList[1].dialogueName = "Xire";
dialogueList[1].isDialogueOption = false;
dialogueList[1].dialogueText[0] = "Run Nyma";
dialogueList[1].dialogueText[1] = "I'm Running Xire";

dialogueList[1].option1 = Null;
dialogueList[1].option2 = Null;

Json Format 1: Json 格式 1:

{
  "Dialogue_Nyma": [
    {
      "dialogueName": "Nyma",
      "isDialogueOption": true,
      "dialogueText": [
        "Hi Xire! how are you?",
        "Hi Nyma! i'm fine and you?"
      ],
      "Option1": "Fine!",
      "Option2": "i'm not fine!"
    }
  ],
  "Dialogue_Xire": [
    {
      "dialogueName": "Xire",
      "isDialogueOption": false,
      "dialogueText": [
        "Run Nyma!",
        "i'm Running Xire."
      ],
      "Option1": null,
      "Option2": null
    }
  ]
}

Json format 2: JSON格式2:

[
  {
    "dialogueName": "Nyma",
    "isDialogueOption": true,
    "dialogueText": [
      "Hi Xire! how are you?",
      "Hi Nyma! i'm fine and you?"
    ],
    "Option1": "Fine!",
    "Option2": "i'm not fine!"
  },
  {
    "dialogueName": "Xire",
    "isDialogueOption": false,
    "dialogueText": [
      "Run Nyma!",
      "i'm Running Xire."
    ],
    "Option1": null,
    "Option2": null
  }
]

If someone could help me to find a way to deserialize one of these json formats to populate my list i'll be really thankful!如果有人可以帮助我找到一种方法来反序列化这些 json 格式之一以填充我的列表,我将非常感激!

i also tried to create a class that contains a array of Dialogue List我还尝试创建一个包含对话列表数组的类

[System.Serializable]
public class DialogueListCollection
{
    public DialogueList[] dialogueList;
}

and tried to parse like this并尝试像这样解析

string path = "DialogueJson/Textos";
var contents = Resources.Load<TextAsset>(path);
dialogueList = JsonConvert.DeserializeObject<DialogueListCollection>(contents.text);

but didn't work.但没有用。

Your class should represent the dialog:你的类应该代表对话框:

public class Dialogue
{
    public string dialogueName;
    public bool isDialogueOption;
    public string[] dialogueText;

    public string option1;
    public string option2;
}

(Notice the "List" is gone from the class name) (注意“列表”从类名中消失了)

You can then use Newtonsoft to deserialize it to an array:然后,您可以使用 Newtonsoft 将其反序列化为数组:

var json = @"[
  {
    ""dialogueName"": ""Nyma"",
    ""isDialogueOption"": true,
    ""dialogueText"": [
      ""Hi Xire! how are you?"",
      ""Hi Nyma! i'm fine and you?""
    ],
    ""Option1"": ""Fine!"",
    ""Option2"": ""i'm not fine!""
  },
  {
    ""dialogueName"": ""Xire"",
    ""isDialogueOption"": false,
    ""dialogueText"": [
      ""Run Nyma!"",
      ""i'm Running Xire.""
    ],
    ""Option1"": null,
    ""Option2"": null
  }
]";

Dialogue[] list = JsonConvert.DeserializeObject<Dialogue[]>(json);

Your array list now contains two entries.您的数组list现在包含两个条目。

The second format you provided is correctly formatted and used in this sample.您提供的第二种格式已正确格式化并在本示例中使用。

Add class that will contain only array of DialogueList and parse json as this class.添加仅包含 DialogueList 数组的类,并将 json 解析为此类。 You'll need to add one field in json to put list in it.您需要在 json 中添加一个字段以将列表放入其中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM