簡體   English   中英

使用C#將JSON反序列化到類

[英]JSON deserialization to class with C#

使用以下命令驗證JSON: http ://jsonlint.com/並使用以下命令創建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": ""
}}

Invoice不是您的JSON的頂級課程。 您應該使用RootObject

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

要獲取Invoice ret.invoice在反序列ret.invoice后使用ret.invoice

使用從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;

好的,因為你試圖反序列化為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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM