gpt4 book ai didi

c# - Json字符串转C#对象

转载 作者:行者123 更新时间:2023-11-30 13:38:21 24 4
gpt4 key购买 nike

我正在尝试将 json 字符串解析为 C# 类对象。我在 c# 中有三个类,我从服务中获取 json 字符串,我正在编码以将此 json 字符串存储到 sql 表中。我在将 json 字符串解析为 c# 中的多个类时遇到问题。我正在使用 newtonsoft.json 进行解析。我有以下 json 字符串

{
"receipt_cr": {
"VchNo": [
"CR::00001",
"CR::00002",
"CR::00003",
"CR::00004",
"CR::00005"
],
"VoucherName": [
"Receipt",
"Receipt",
"Receipt",
"Receipt",
"Receipt"
],
"VchDate": [
"2013-04-02 00:00:00",
"2013-04-02 00:00:00",
"2013-04-02 00:00:00",
"2013-04-02 00:00:00",
"2013-04-02 00:00:00"
],
"LedgerName": [
"xxxxxx",
"yyyyy",
"ssssss",
"rrrrrrr",
"wwwwwww"
],
"UnderGroup": [
"Sundry Debtors",
"Sundry Debtors",
"Sundry Debtors",
"Sundry Debtors",
"Sundry Debtors"
],
"AgnstRefNumber": [
"",
"",
"",
"",
""
],
"NetAmount": [
"2973500.00",
"2973500.00",
"5967015.00",
"8968432.00",
"5980000.00"
],
"AgnstReferenceAmount": [
"",
"",
"",
"",
""
],
"CostCentreName": [
"",
"",
"",
"",
""
],
"CostCategory": [
"",
"",
"",
"",
""
],
"Status ": [
"NI",
"NI",
"NI",
"NI",
"NI"
]
},
"receipt_dr": {
"VchNo": [
"CR::00001",
"CR::00002",
"CR::00003",
"CR::00004",
"CR::00004"
],
"VoucherName": [
"Receipt",
"Receipt",
"Receipt",
"Receipt",
"Receipt"
],
"VchDate": [
"2013-04-02 00:00:00",
"2013-04-02 00:00:00",
"2013-04-02 00:00:00",
"2013-04-02 00:00:00",
"2013-04-02 00:00:00"
],
"CashBankLedger": [
"Bank",
"Bank",
"Bank",
"Bank",
"Bank"
],
"UnderGroup": [
"BankAccount",
"BankAccount",
"BankAccount",
"BankAccount",
"BankAccount"
],
"Amount": [
"2973500.00",
"2973500.00",
"5967015.00",
"2989477.00",
"2989477.00"
],
"TransactionType": [
"cheque",
"RTGS",
"cheque",
"cheque",
"cheque"
],
"InstrumentNo": [
"1",
"2",
"3",
"4",
"5"
],
"Date": [
"2013-04-02 00:00:00",
"2013-04-02 00:00:00",
"2013-04-02 00:00:00",
"2013-04-02 00:00:00",
"2013-04-02 00:00:00"
],
"BankName": [
"Axis - 230",
"Axis - 230",
"Axis - 230",
"Axis - 230",
"Axis - 230"
],
"BankBranch": [
"Avinashi Road",
"Avinashi Road",
"Avinashi Road",
"Avinashi Road",
"Avinashi Road"
],
"BankDate": [
"2013-04-02 00:00:00",
"2013-04-02 00:00:00",
"2013-04-02 00:00:00",
"2013-04-02 00:00:00",
"2013-04-02 00:00:00"
],
"Narration": [
"",
"",
"",
"",
""
]
},
"payment_dr": {
"VchNo": [
"SP::00001",
"SP::00002",
"SP::00003",
"SP::00004",
"SP::00005"
],
"VoucherName": [
"Payment",
"Payment",
"Payment",
"Payment",
"Payment"
],
"VchDate": [
"2013-04-02 13:49:00",
"2013-04-02 13:53:00",
"2013-04-02 13:54:00",
"2013-04-02 13:55:00",
"2013-04-02 13:55:00"
],
"LedgerName": [
"shruthi jewel city",
"TRUST TECHONOLOGIES",
"RKR Gold Pvt Ltd",
"Bank of Nova Scotia",
"Bank of Nova Scotia"
],
"UnderGroup": [
"SundryCredit",
"SundryCredit",
"SundryCredit",
"SundryCredit",
"SundryCredit"
],
"NetAmount": [
"8977387",
"1000",
"14915000",
"14700000",
"27700000"
],
"AgnstRefNumber ": [
"",
"",
"",
"",
""
],
"AgnstReferenceAmount ": [
"",
"",
"",
"",
""
],
"CostCentreName ": [
"",
"",
"",
"",
""
],
"CostCategory ": [
"",
"",
"",
"",
""
],
"Status ": [
"NI",
"NI",
"NI",
"NI",
"NI"
]
}

}

我的 C# 类就像

 public class ReceiptCr
{
public String VchNo { get; set; }
public String VoucherName { get; set; }
public DateTime VchDate { get; set; }
public String LedgerName { get; set; }
public String UnderGroup { get; set; }
public Decimal NetAmount { get; set; }
public String AgnstRefNumber { get; set; }
public Decimal AgnstReferenceAmount { get; set; }
public String CostCentreName { get; set; }
public String CostCategory { get; set; }
public String Status { get; set; }
}
public class ReceiptDr
{
public String VchNo { get; set; }
public String VoucherName { get; set; }
public DateTime VchDate { get; set; }
public String CashOrBankLedger { get; set; }
public String UnderGroup { get; set; }
public Decimal Amount { get; set; }
public String TransactionType { get; set; }
public String InstrumentNo { get; set; }
public DateTime BankDate { get; set; }
public String BankName { get; set; }
public String BankBranch { get; set; }
public String Narration { get; set; }
}
public class PaymentDr
{
public String VchNo { get; set; }
public String VoucherName { get; set; }
public DateTime VchDate { get; set; }
public String LedgerName { get; set; }
public String UnderGroup { get; set; }
public Decimal NetAmount { get; set; }
public String AgnstRefNumber { get; set; }
public Decimal AgnstReferenceAmount { get; set; }
public String CostCentreName { get; set; }
public String CostCategory { get; set; }
public String Status { get; set; }
}

如何在 C# 中将这个 json 字符串解析为这些类

最佳答案

以下结构应该可以帮助您入门:

public class ReceiptCR
{
public string[] VchNo { get; set; }
public string[] VoucherName { get; set; }
public string[] VchDate { get; set; }
...
}

public class ReceiptDR
{
public string[] VchNo { get; set; }
public string[] VoucherName { get; set; }
public string[] VchDate { get; set; }
...
}

public class PaymentDR
{
public string[] VchNo { get; set; }
public string[] VoucherName { get; set; }
public string[] VchDate { get; set; }
...
}

然后定义包装器:

public class Root
{
public ReceiptCR Receipt_cr { get; set; }
public ReceiptDR Receipt_dr { get; set; }
public PaymentDR Payment_dr { get; set; }
}

您可以从 json 字符串反序列化:

string json = ...
Root result = JsonConvert.DeserializeObject<Root>(json);

关于c# - Json字符串转C#对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17358374/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com