首页 > 解决方案 > 如何序列化列表> 在 C#/Unity 中转换为 JSON

问题描述

刚刚开始使用 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的输出:

{}

任何帮助将非常感激!

标签: c#jsonunity3d

解决方案


正如您提到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();
            }

        }

序列化对象的代码片段

 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);


推荐阅读