Json字符串到c#对象

时间:2013-06-28 06:16:25

标签: c# json

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

中的这些类

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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);