c# - 如何序列化列表> 在 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的输出:
{}
任何帮助将非常感激!
解决方案
正如您提到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);
推荐阅读
- arrays - 将数组数据转换为 bmp
- sql-server-2008 - 由于未连接套接字且未提供地址,因此不允许发送或接收数据的请求 - SSIS 2008
- php - 发送电子邮件的 Web 表单不发送到 gmail
- python - Pyusb 无法识别 USB 设备(python 3.9.6 和 windows)
- discord.js - Discord.JS 录音大 pcm 文件
- jetbrains-ide - 是否可以拆分窗口而不在中间显示选项卡?
- timer - AM572x 定时器在调试器暂停期间继续计数
- laravel - PhpStorm 中的代码提示
- node.js - Nuxt 在发送到 AWS S3 之前将图像转换为 webp
- amazon-s3 - 快照 IBM Cloud 对象存储