3

我正在尝试将 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; }
    }

如何将此json字符串解析为c#中的这些类

4

2 回答 2

9

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

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);
于 2013-06-28T06:25:56.650 回答
1

根据您的版本,.NET您可以利用JavaScriptSerializer

如果你稍微改变你的课程

public class ReceiptContainer
{
    public ReceiptCr receipt_cr;
    public ReceiptDr receipt_dr;
    public PaymentDr payment_dr;

    public ReceiptContainer() 
    {
        receipt_cr = new ReceiptCr();
        receipt_dr = new ReceiptDr();
        payment_dr = new PaymentDr();
    }

    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 ReceiptCr() { }
    }
    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 ReceiptDr() { }
    }
    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; }
        public PaymentDr() { }
    }
}

然后根据您的版本,.NET您可以使用

ReceiptContainer receipts = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<ReceiptContainer>(yourJsonString);
于 2013-06-28T06:48:07.247 回答