我正在尝试使用 Json.NET 来序列化一个子类。生成的 json 包含超类的序列化属性,但不包含子类对象的属性。
这似乎与我在 SO 上发现的问题有关。但是不得不写一个JsonConverter
似乎有点矫枉过正。
示例子类:
public class MySubclass : List<string>
{
public string Name { get; set; }
}
序列化示例:
MySubclass myType = new MySubclass() { Name = "Awesome Subclass" };
myType.Add("I am an item in the list");
string json = JsonConvert.SerializeObject(myType, Newtonsoft.Json.Formatting.Indented);
生成的json:
[
"I am an item in the list"
]
我预计结果会更像这样:
{
"Name": "Awesome Subclass",
"Items": [
"I am an item in the list"
]
}
也许我只是在序列化时没有使用正确的配置。有人有什么建议吗?