【发布时间】:2020-05-17 16:09:08
【问题描述】:
刚开始使用 JSON,却被困在嵌套列表中。
我要序列化的类是这样的:
[Serializable]
public class TestMainClass
{
public List<List<Information>> additionalInformation = new List<List<Information>>();
}
[Serializable]
public class Information
{
public varA;
public varB;
}
类是如何被初始化的:
TestMainClass mainClass = new TestMainClass();
//Build first list of information
List<Information> firstListOfInfo = new list<Information>;
Information infoA = new Information()
{
varA = a,
varB = b,
}
Information infoB = new Information()
{
varA = c,
varB = d,
}
firstListOfInfo.Add(infoA);
firstListOfInfo.Add(infoB);
//Build other lists of information
.
.
.
//Add Lists to mainClass List
mainClass.additionalInformation.Add(firstListOfInfo);
//Add other list to the mainClass list
.
.
.
类是如何被序列化的:
JsonUtility.ToJson(mainClass);
Json 的输出:
{}
任何帮助将不胜感激!
【问题讨论】:
-
您正在使用字段。它们必须是属性(添加
{ get; set; })。 -
@CodeCaster 不,他们没有......大多数时候甚至反过来...... Unity不会序列化属性......只有字段
-
@Matt this
[Serializable] public class Information { public varA; public varB; }is no valid c# ...这些字段的类型是什么?它们是可序列化的类型吗? -
@derHugo 好吧,我完全错过了使用 Unity。