如何在JSON中显示对象数组?

ruarlubt  于 2023-04-22  发布在  其他
关注(0)|答案(2)|浏览(92)

我有一个名为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 
    }
  ]
}
7jmck4yq

7jmck4yq1#

默认情况下,仅序列化public属性。
将访问器从private更改为public

public DocumentItem[] Docs { get; set; } = null!;
// ^^^ here !!
sg24os4d

sg24os4d2#

{
            public class Documents
            {
                [BsonId]
                [JsonIgnore]
                public ObjectId DocumentsId { get; set; }
        
        
                public string Id { get => DocumentsIdToString(); set { DocumentsIdToString(); } }
        

                public string Title { get; set; } = null!;
        
               
                public string Icon { get; set; } = null!;
        
              
                public string Link { get; set; } = null!;
        
                public List<DocumentItem> Docs { get; set; };
        
        public Documents (int id,string title,string icon,string link,List<DocumentItem> list)
    {
    Id = id,
    Title = title,
    Icon=icon,
    Docs = list.Select(x=>new DocumentItem(x.Name, x.Prefix, x.Module)).ToList();
    
    }
    
    /* UseLess
                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; }
        
                public string Prefix { get; set; } 
        
                public int Module { get; set; }
        
                public DocumentItem(string Name, string Prefix, int Module)
                {
                    this.Name = Name;
                    this.Prefix = Prefix;
                    this.Module = Module;
                }
        
                
            }
        }

试试这个

相关问题