Answer the question
In order to leave comments, you need to log in
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>();
}
}
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
don't use json from unity, use newtonsoft
https://github.com/jilleJr/Newtonsoft.Json-for-Unity
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question