Z
Z
zeni1agent2022-03-28 18:00:55
Unity
zeni1agent, 2022-03-28 18:00:55

Why can't I save List to json?

I am trying to convert data to json but why list is not saved?

public class NewDialog
{
  public string Name,Textarea;
  public Sprite MySprite;
  
  public NewDialog( string textarea, string name = null, string SpritePath = null)
  {
    Textarea = textarea;
    Name = name;
    if(SpritePath != null)
      MySprite = Resources.Load<Sprite>(SpritePath);
  }
}


public class ArrayDialog{
  
  public List<NewDialog> MyListDialog;
  public string Name;
  public ArrayDialog(string name)
  {
    Name = name;
    MyListDialog = new List<NewDialog>();
  }
  
}


Creating a dialogue
ArrayDialog arraydialog1 = new ArrayDialog("first dialog");
    
    arraydialog1.MyListDialog.Add(new NewDialog("blablabla1", "player1", "Sprites/Cards/ebalo"));
    arraydialog1.MyListDialog.Add(new NewDialog("blablabla2"));
    
    DialogManagerList.AllDialogs.Add(arraydialog1);
    
    
    ArrayDialog arraydialog2 = new ArrayDialog("second dialog");
    
    arraydialog2.MyListDialog.Add(new NewDialog("blablabla1", "player2", "Sprites/Cards/ebalo"));
    arraydialog2.MyListDialog.Add(new NewDialog("blablabla2", "player2", "Sprites/Cards/ebalo"));
    
    DialogManagerList.AllDialogs.Add(arraydialog2);
    
    
    string jsonDataString = JsonUtility.ToJson(arraydialog1, true);
    
    Debug.Log("DIALOG");
    Debug.Log(jsonDataString);
    
    var  bibi =  JsonUtility.FromJson<ArrayDialog>(jsonDataString);
    
    
    foreach (var vary in arraydialog1.MyListDialog)
    {
      Debug.Log(vary.Textarea);
    }
    
    
    foreach (var vary in bibi.MyListDialog)
    {
      Debug.Log(vary.Textarea);
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
Farawa, 2022-03-28
@Farawa

don't use json from unity, use newtonsoft
https://github.com/jilleJr/Newtonsoft.Json-for-Unity

F
freeExec, 2022-03-28
@freeExec

Because it is necessary to indicate that the class[Serializable]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question