json 如何创建C#对象并序列化它

e4eetjau  于 2023-05-08  发布在  C#
关注(0)|答案(3)|浏览(199)

在C#中,我想用下面的格式创建JSON字符串或对象:

{
    "retailer_br_id" : "5473182",
    "retailer_external_id": "",
    "erp_invoice_number" : "INV-202302",
    "invoice_date": "2023-04-06T17:00:00.000Z",
    "status" : 1,
    "details":[{
        "sku_external_id": "TD5015151432201",
        "quantity" : "2",
        "price_per_item": "14.5"
        },
        {
            "sku_external_id": "000005020170898446",
            "quantity" : 1,
            "price_per_item": 10.50
        }
    ]
}

我尝试了以下方法,但似乎不起作用,它只给我dList值.......................................................................................................................................................................................................................................

DataTable dt = new DataTable("Details");
dt.Columns.Add("sku_external_id", typeof(string));

//Data  
dt.Rows.Add("0000000000");
dt.Rows.Add("111111111");
dt.Rows.Add("1222222222");
dt.Rows.Add("3333333333");

Root rt = new Root();
rt.retailer_br_id = "32423432432424";
rt.retailer_external_id = "";
rt.erp_invoice_number = "PBY-202304";
DateTime d = DateTime.Now;
string dateString = d.ToString("Y-m-d H:i:s");
rt.invoice_date = dateString;

List<Detail> dList = new List<Detail>();

for (int i = 0; i < dt.Rows.Count; i++)
{
    Detail det = new Detail();
    
    det.sku_external_id = (string)dt.Rows[i]["sku_external_id"];
    
    dList.Add(det);
}

string json = JsonConvert.SerializeObject(dList, Formatting.Indented);
textBox1.Text = json;
06odsfpq

06odsfpq1#

1.您正在序列化dList,但不是root,这就是为什么它显示序列化的dList
1.缺少将dList分配给rt.details

rt.details = dList;
        
string json = JsonConvert.SerializeObject(rt, Formatting.Indented);

1.要将日期序列化为ISO 8601格式,请执行以下操作:

string dateString = d.ToString("yyyy-MM-ddTHH:MM:sss.fffZ");

备选方案

1.不使用foreach循环迭代并将项添加到dList中,您可以使用以下方式实现:

dList = JArray.FromObject(dt)
    .ToObject<List<Detail>>();

1.将属性命名为Pascal Case,并使用JsonProperty属性定义序列化和反序列化中使用的属性名称。

public class Detail 
{ 
    [JsonProperty("sku_external_id")] 
    public string SkuExternalId { get; set; }

    [JsonProperty("quantity")]
    public object Quantity { get; set; }

    [JsonProperty("price_per_item")]
    public object PricePerItem { get; set; }
}

public class Root
{
    [JsonProperty("retailer_br_id")]
    public string RetailerBrId { get; set; }

    [JsonProperty("retailer_external_id")]
    public string RetailerExternalId { get; set; }

    [JsonProperty("erp_invoice_number")]
    public string ErpInvoiceNumber { get; set; }

    [JsonProperty("invoice_date")]
    public DateTime InvoiceDate { get; set; }

    [JsonProperty("status")]
    public int Status { get; set; }

    [JsonProperty("details")]
    public List<Detail> Details { get; set; }
}

1.将invoice_date属性更改为DateTime类型,以便它将序列化并显示ISO 8601中的格式,而不是手动定义格式。

rt.invoice_date = DateTime.Now;
[JsonProperty("invoice_date")]
public DateTime invoice_date { get; set; }
nhaq1z21

nhaq1z212#

json将只有dList的值,因为您还没有将rt分配给det对象。我假设det有一个属性,可以用rt的值赋值,例如:

for (int i = 0; i < dt.Rows.Count; i++)
{
    Detail det = new Detail();
                
    det.sku_external_id = (string)dt.Rows[i]["sku_external_id"];
    det.root = rt;
    
    dList.Add(det);
}

如果det没有root类型属性,则需要添加一个。
您可能不希望将root分配给列表中det的每个示例。如果你想在列表外分配它,那么你需要一个 Package 器对象。

class MyObject {
   public  Root root{ get; set; }
   public  List<Detail> dList { get; set; }
}

修改后的代码看起来像...

MyObject myobject = new MyObject();
List<Detail> dList = new List<Detail>();
for (int i = 0; i < dt.Rows.Count; i++)
{
    Detail det = new Detail();

    det.sku_external_id = (string)dt.Rows[i]["sku_external_id"];

    dList.Add(det);
}
myobject.root = myobject;
myobject.dlist = dList;

string json = JsonConvert.SerializeObject(myobject, Formatting.Indented);
textBox1.Text = json;
jdgnovmf

jdgnovmf3#

你可以使用匿名类型

var obj = new
    {
        retailer_br_id = 5473182,
        retailer_external_id = 0,
        erp_invoice_number = "INV-202302",
        invoice_date = "2023-04-06T17=00=00.000Z",
        status = 1,
        details = new[] { 
        new {
        sku_external_id= "TD5015151432201",
        quantity = 2,
        price_per_item= 14.5
        },
        new {
            sku_external_id= "000005020170898446",
            quantity = 1,
            price_per_item= 10.50
        }
      }
    };

string json = JsonConvert.SerializeObject(obj, Newtonsoft.Json.Formatting.Indented);

现在,如果你愿意,你可以使用这个链接将这个JSON在线转换为C#类,例如https://json2csharp.com/

相关问题