嵌套Json结构.NET

问题描述 投票:0回答:1

我想在我的.NET代码中实现以下JSON结构,但似乎无法从数据库中正确创建数组[Attributes]

您如何最好地设置嵌套Json这样?

{
    "productid": 0,
    "active": true,
    "activepos": true,
    "title": "Test",
    "sku": "7777766666",
    "deliverystatus": "",
    "moneyprice": "1590.00",
    "moneypriceorg": "0.00",
    "moneypricein": "8.00",
    "moneyofferprice": "0.00",
    "friendly": "This is Friendly",
    "barcode": "",
    "attributes": [ "Casual", "Green" ]
}

这是我的课程,我应该为此单独开设一个课程吗?以及如何加入类以构建嵌套的JSON?

public class ProductSQL
{
        public string sku { get; set; } 
        public string title { get; set; }
        public string description { get; set; }
        public string moneypricein { get; set; }
        public string moneyprice { get; set; }
        public string ean { get; set; }
        public string [] attributes { get; set; }
}

在我的SQL数据库中,我有五列attribute 1, attribute 2, attribute 3, attribute 4, attribute 5

我为具有五个列的属性假定一个单独的类,然后应将其串联起来?

关于最佳实践JSON构建的任何建议,将不胜感激。这是看跌期权。

c# .net json put
1个回答
0
投票

最佳实践(仅我的看法)是将数据库中的所有字段(即SELECT正在处理的所有字段)都放在C#类上,而不是将它们收集到数组中。

使用SQL DB的东西是,一行代表数据单位。如果您需要存储与该单位相关的列表或某些数据的数组,则必须为此使用单独表

因此,根据您的attribute1attribute2字段的上下文含义,您应该选择其中一个

  • 使用第一个表中的外键创建另一个表(以存储属性),或者,>>
  • 如果所有属性都是固定的,并且确实属于每个“单元”,并且具有简单的单列表示(或复杂的可序列化),则您的方法可能有用。
  • 但是,从您如何不命名任何属性的角度来看,我怀疑第二个选择对您来说不是一个选择。

它仍然取决于这些属性的含义,但是如果您将它们命名为...1...2等,则表示不良设计

如果您尝试将一行中的各列收集到一个数组中,则这是不良设计

的强烈标志。

[当遇到不良的设计标志时,最好的办法是从逻辑上重新考虑您想要的是什么。

© www.soinside.com 2019 - 2024. All rights reserved.