简体   繁体   中英

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.

i would like to populate my list (of this class below) using one of this 2 Json formats.

[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:

{
  "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:

[
  {
    "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!

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:

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.

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. You'll need to add one field in json to put list in it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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