Question

Validated the JSON using: http://jsonlint.com/ and created the C# classes using: json2csharp.com.

I've tried deserializing with JSON.Net, RestSharp, .Net DataContractJsonSerializer and JavaScriptSerializer, but none work. The class is always empty and no errors are thrown.

I enabled debugging with JSON.Net as specified here: http://blog.mrlacey.co.uk/2012/03/debugging-deserialization-errors-in.html, but no errors are thrown.

I've also tried with a simplified Invoice class (no lists) as all I need are a few of the basic properties.

Any ideas on what I'm doing wrong would be appreciated.

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

This is the JSON being returned via RestSharp in .Content.

{
"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": ""
}}
Était-ce utile?

La solution

Invoice is not the top level class for your JSON. You should use RootObject instead:

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

To get Invoice use ret.invoice after deserialization.

Autres conseils

Using the classes generated from http://json2csharp.com, you should be deserializing to a RootObject, not to an 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;

Ok it's because you're trying to deserialize into an Invoice but it's actually nested within another object in the json;

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

Then do;

  Wrapper wrap = JsonConvert.DesrializeObject<Wrapper>(results.Contect);
  if (rwap != null)
      Invoice i = wrap.Invoice;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top