使用C#将JSON反序列化到类

时间:2014-02-21 17:40:07

标签: c# json json-deserialization

使用:http://jsonlint.com/验证JSON并使用以下命令创建C#类:json2csharp.com。

我尝试使用JSON.Net,RestSharp,.Net DataContractJsonSerializer和JavaScriptSerializer进行反序列化,但都没有工作。该类始终为空,不会抛出任何错误。

我按照此处指定的JSON.Net启用了调试:http://blog.mrlacey.co.uk/2012/03/debugging-deserialization-errors-in.html,但不会抛出任何错误。

我也尝试过简化的Invoice类(没有列表),因为我需要的只是一些基本属性。

任何关于我做错事的想法都会受到赞赏。

Invoice ret = JsonConvert.DeserializeObject<Invoice>(results.Content);

这是通过.Content中的RestSharp返回的JSON。

{
"code": 0,
"message": "success",
"invoice": {
    "invoice_id": "464323000000255001",
    "invoice_number": "993301",
    "date": "2014-03-01",
    "status": "draft",
    "payment_terms": 0,
    "payment_terms_label": "Due on Receipt",
    "due_date": "2014-03-01",
    "payment_expected_date": "",
    "last_payment_date": "",
    "reference_number": "",
    "customer_id": "464323000000194095",
    "customer_name": "User, Test",
    "contact_persons": [],
    "currency_id": "464323000000005001",
    "currency_code": "USD",
    "currency_symbol": "$",
    "exchange_rate": 1,
    "discount": 0,
    "is_discount_before_tax": true,
    "discount_type": "item_level",
    "recurring_invoice_id": "",
    "is_viewed_by_client": false,
    "line_items": [
        {
            "line_item_id": "464323000000255009",
            "item_id": "464323000000047609",
            "project_id": "",
            "time_entry_ids": "",
            "expense_id": "",
            "expense_receipt_name": "",
            "name": "Management Fees",
            "description": "Testing invoice update",
            "item_order": 0,
            "bcy_rate": 1,
            "rate": 1,
            "quantity": 1,
            "unit": "",
            "discount_amount": 0,
            "discount": 0,
            "tax_id": "",
            "tax_name": "",
            "tax_type": "tax",
            "tax_percentage": 0,
            "item_total": 1
        },
        {
            "line_item_id": "464323000000255011",
            "item_id": "464323000000047609",
            "project_id": "",
            "time_entry_ids": "",
            "expense_id": "",
            "expense_receipt_name": "",
            "name": "Management Fees",
            "description": "Another test",
            "item_order": 1,
            "bcy_rate": 2,
            "rate": 2,
            "quantity": 1,
            "unit": "",
            "discount_amount": 0,
            "discount": 0,
            "tax_id": "",
            "tax_name": "",
            "tax_type": "tax",
            "tax_percentage": 0,
            "item_total": 2
        }
    ],
    "shipping_charge": 0,
    "adjustment": 0,
    "adjustment_description": "Adjustment",
    "late_fee": {
        "name": "",
        "type": "percentage",
        "rate": 0,
        "amount": 0,
        "frequency_type": "month"
    },
    "sub_total": 3,
    "tax_total": 0,
    "total": 3,
    "taxes": [],
    "payment_reminder_enabled": true,
    "payment_made": 0,
    "credits_applied": 0,
    "tax_amount_withheld": 0,
    "balance": 3,
    "write_off_amount": 0,
    "allow_partial_payments": false,
    "price_precision": 2,
    "payment_options": {
        "payment_gateways": []
    },
    "is_emailed": false,
    "reminders_sent": 0,
    "last_reminder_sent_date": "",
    "billing_address": {
        "address": "",
        "city": "",
        "state": "",
        "zip": "",
        "country": "",
        "fax": ""
    },
    "shipping_address": {
        "address": "",
        "city": "",
        "state": "",
        "zip": "",
        "country": "",
        "fax": ""
    },
    "notes": "Thanks for your business.",
    "terms": "",
    "custom_fields": [],
    "template_id": "464323000000025001",
    "template_name": "Standard Template",
    "template_type": "standard",
    "created_time": "2014-02-21T08:39:11-0500",
    "last_modified_time": "2014-02-21T08:39:11-0500",
    "attachment_name": "",
    "can_send_in_mail": true,
    "salesperson_id": "",
    "salesperson_name": "",
    "invoice_url": ""
}}

3 个答案:

答案 0 :(得分:2)

Invoice不是您的JSON的顶级课程。您应该使用RootObject代替:

var ret = JsonConvert.DeserializeObject<RootObject>(results.Content);

在反序列化后让Invoice使用ret.invoice

答案 1 :(得分:0)

使用从http://json2csharp.com生成的类,您应该反序列化为RootObject,而不是Invoice

public class RootObject
{
    public int code { get; set; }
    public string message { get; set; }
    public Invoice invoice { get; set; }
}

Invoice ret = JsonConvert.DeserializeObject<RootObject>(results.Content).invoice;

答案 2 :(得分:0)

好的,因为你试图反序列化为Invoice,但实际上它嵌套在json中的另一个对象中;

public class Wrapper
{
     public Invoice invoice { get; set; }
     public int code { get; set; }
     public int message { get; set; }
}

然后做;

  Wrapper wrap = JsonConvert.DesrializeObject<Wrapper>(results.Contect);
  if (rwap != null)
      Invoice i = wrap.Invoice;