2014-02-21 4 views
0

JSON을 사용하여 http://jsonlint.com/을 확인하고 json2csharp.com을 사용하여 C# 클래스를 만들었습니다.JSON을 C#으로 클래스에 비 직렬화

JSON.Net, RestSharp, .Net DataContractJsonSerializer 및 JavaScriptSerializer로 deserialize를 시도했지만 아무 것도 작동하지 않았습니다. 클래스는 항상 비어 있으며 오류가 발생하지 않습니다.

여기에 지정된대로 JSON.Net을 사용하여 디버깅을 활성화했지만 오류는 발생하지 않았습니다.

기본 속성 몇 가지 만 있으면 필요한 간단한 Invoice 클래스 (목록 없음)를 사용해 보았습니다.

내가 잘못하고있는 것에 대한 아이디어는 인정 될 것입니다.

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

RestSharp를 통해 .Content에 반환되는 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": "" 
}} 

답변

2

Invoice은 JSON의 최상위 클래스가 아닙니다. 대신 RootObject를 사용해야합니다

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

직렬화 복원 후 Invoice 사용 ret.invoice을 얻을 수 있습니다.

+0

감사합니다. Json.Net만을 사용할 때 이것을 처음 시도했는데 "입력 문자열이 올바른 형식이 아니 었습니다"라는 오류가 발생하여 다른 시도를 수행했습니다. 입력 문자열 문제는 속성 중 하나가 Int이고 값이 10 진수로 인해 발생했습니다. 그 시점에서 근시가 시작되었고 나는 나무 숲을 볼 수 없었습니다 ... – topry

0

http://json2csharp.com에서 생성 된 클래스를 사용하면 Invoice이 아닌 RootObject으로 직렬화를 해제해야합니다.

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; 
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; 
관련 문제