我有一个名为Documents的类,它应该有一个DocumentItem对象的数组,我希望它们出现在我想发送到数据库的json中,但它们没有。我希望后端在json中显示数组,它将像任何其他字段一样进入数据库。
Json看起来是这样的
{
"id": "string",
"title": "string",
"icon": "string",
"link": "string"
}
{
public class Documents
{
[BsonId]
[JsonIgnore]
public ObjectId DocumentsId { get; set; }
public string Id { get => DocumentsIdToString(); set { DocumentsIdToString(); } }
[Required]
public string Title { get; set; } = null!;
[Required]
public string Icon { get; set; } = null!;
[Required]
public string Link { get; set; } = null!;
private DocumentItem[] Docs { get; set; } = null!;
public Documents()
{
Docs = new DocumentItem[]
{
new DocumentItem("Capex Request","CX", 1),
new DocumentItem("Cash Request","CR", 1),
new DocumentItem("Stores Request","SR", 1),
new DocumentItem("Quotation","QT", 1)
};
}
private string DocumentsIdToString()
{
return DocumentsId.ToString();
}
}
}
public class DocumentItem
{
public string Name { get; set; } = null!;
public string Prefix { get; set; } = null!;
public int Module { get; set; }
public DocumentItem(string Name, string Prefix, int Module)
{
this.Name = Name;
this.Prefix = Prefix;
this.Module = Module;
}
}
}
我希望Json像这样出现
{
"id": "string",
"title": "string",
"icon": "string",
"link": "string",
"docs": [
{
"name": "Capex Request",
"prefix": "CX",
"module": 1
},
{
"name": "Cash Request",
"prefix": "CR",
"module": 1
},
{
"name": "Stores Request",
"prefix": "SR",
"module": 1
},
{
"name": "Quotation",
"prefix": "QT",
"module": 1
}
]
}
2条答案
按热度按时间7jmck4yq1#
默认情况下,仅序列化
public
属性。将访问器从
private
更改为public
。sg24os4d2#
试试这个