【问题标题】:How to Serialize List<List<T>> into JSON in C# / Unity如何在 C# / Unity 中将 List<List<T>> 序列化为 JSON
【发布时间】: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。

标签: c# json unity3d


【解决方案1】:

正如你提到的Newtonsoft.Json nuget 包不能在你的情况下使用然后考虑你有以下类要序列化

[Serializable]
    public class TestMainClass
    {
        public List<List<Information>> additionalInformation = new List<List<Information>>();
    }

    [Serializable]
    public class Information
    {
        public string A;
        public string B;
    }

您需要根据需要使用和自定义序列化程序,我尝试将下面的序列化程序与Newtonsoft.Json 匹配,但如果您需要,您可以修改它。

   public class TestMainClassConverter : System.Text.Json.Serialization.JsonConverter<TestMainClass>
        {
            public override TestMainClass Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
            {
                throw new NotImplementedException();
            }

            public override void Write(Utf8JsonWriter writer, TestMainClass testMain, JsonSerializerOptions options)
            {

                writer.WriteStartObject();

                writer.WriteStartArray("additionalInformation");
                foreach ( var value in testMain.additionalInformation)
                {
                    foreach (var info in value)
                    {

                        writer.WriteStartObject();

                        // Written this way to show how complex objects can be written/composed with JsonSerializer.
                        writer.WritePropertyName("A");
                        System.Text.Json.JsonSerializer.Serialize(writer, info.A);

                        writer.WritePropertyName("B");
                        System.Text.Json.JsonSerializer.Serialize(writer, info.B);

                        writer.WriteEndObject();
                    }
                }
                writer.WriteEndArray();
                writer.WriteEndObject();
            }

        }

序列化对象的代码sn-p

 TestMainClass mainClass = new TestMainClass();

            //Build first list of information
            List<Information> firstListOfInfo = new List<Information>();
            Information infoA = new Information()
            {
                A = "a",
                B = "b",
            };
            Information infoB = new Information()
            {
                A = "c",
                B = "d",
            };
            firstListOfInfo.Add(infoA);
            firstListOfInfo.Add(infoB);

            mainClass.additionalInformation.Add(firstListOfInfo);

            var options = new JsonSerializerOptions()
            {
                Converters = { new TestMainClassConverter() },
                WriteIndented = true
            };

            string js = System.Text.Json.JsonSerializer.Serialize(mainClass, options);
Console.WriteLine(js);

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-28
  • 2015-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多