JSON .NET反序列化错误的服务器响应

时间:2018-06-30 21:28:24

标签: c# json

我需要反序列化来自FastBill API的JSON响应,(我正在使用JSON .NET),我创建了C#类,它将“保存”从API响应中检索到的数据,问题是服务器响应不正确,如下所示:

"{ \"REQUEST\":{ \"SERVICE\":\"invoice.get\", \"LIMIT\":1, \"FILTER\":{ \"YEAR\":2018 } }, \"RESPONSE\":{ \"INVOICES\":[{ \"INVOICE_ID\":\"11586818\", \"TYPE\":\"outgoing\", \"CUSTOMER_ID\":\"4735248\", \"CUSTOMER_NUMBER\":\"3211\", \"CUSTOMER_COSTCENTER_ID\":\"0\", \"CONTACT_ID\":\"\", \"PROJECT_ID\":\"0\", \"CURRENCY_CODE\":\"EUR\", \"DELIVERY_DATE\":\"02.01.2018\", \"INVOICE_TITLE\":\"Rechnung f\\u00fcr Ihre Bestellung bei Amazon\", \"CASH_DISCOUNT_PERCENT\":\"0.00\", \"CASH_DISCOUNT_DAYS\":\"0\", \"SUB_TOTAL\":22.66, \"VAT_TOTAL\":4.31, \"VAT_ITEMS\":[{ \"VAT_PERCENT\":\"19.00\", \"COMPLETE_NET\":22.66, \"VAT_VALUE\":4.3061344545 }], \"ITEMS\":[{ \"INVOICE_ITEM_ID\":\"33975736\", \"ARTICLE_NUMBER\":\"CK-7I70-IBD4\", \"DESCRIPTION\":\"Gr\\u00fcne Elefanten\\\\'s Bio OPC Traubenkernextrakt mit Bio Acerola Hochdosiert 2 Monatsvorrat - 390mg Kapseln Trauben aus Frankreich\", \"QUANTITY\":\"1.0000\", \"UNIT_PRICE\":\"22.66386555\", \"VAT_PERCENT\":\"19.00\", \"VAT_VALUE\":4.3061344545, \"COMPLETE_NET\":22.66386555, \"COMPLETE_GROSS\":26.9700000045, \"SORT_ORDER\":1 }], \"TOTAL\":26.97, \"ORGANIZATION\":\"\", \"NOTE\":\"\", \"SALUTATION\":\"\", \"FIRST_NAME\":\"NAME\", \"LAST_NAME\":\"NAME\", \"ADDRESS\":\"ADDRESS\", \"ADDRESS_2\":\" \", \"ZIPCODE\":\"97493\", \"CITY\":\"CITY\", \"PAYMENT_TYPE\":\"1\", \"BANK_NAME\":\"\", \"BANK_ACCOUNT_NUMBER\":\"\", \"BANK_CODE\":\"\", \"BANK_ACCOUNT_OWNER\":\"\", \"BANK_IBAN\":\"\", \"BANK_BIC\":\"\", \"COUNTRY_CODE\":\"DE\", \"VAT_ID\":\"\", \"TEMPLATE_ID\":\"963360\", \"INVOICE_NUMBER\":\"3209\", \"INTROTEXT\":\"wir bedanken uns f\\u00fcr Ihre Bestellung bei Amazon (Bestellnummer 306-9638137-3397961). Der Zahlungsbetrag wurde bereits entrichtet.\", \"PAID_DATE\":\"2018-01-02 00:00:00\", \"IS_CANCELED\":\"0\", \"INVOICE_DATE\":\"2018-01-02\", \"DUE_DATE\":\"0000-00-00 00:00:00\", \"PAYMENT_INFO\":\"01\\\/02\\\/2018 26, 97 \\u20ac () \", \"PAYMENTS\":[{ \"PAYMENT_ID\":\"7640244\", \"DATE\":\"01\\\/02\\\/2018\", \"AMOUNT\":\"26.97\", \"CURRENCY_CODE\":\"EUR\", \"NOTE\":\"\", \"TYPE\":\"\" }], \"LASTUPDATE\":\"2018-01-02 17:29:21\", \"DOCUMENT_URL\":\"https:\\\/\\\/my.fastbill.com\\\/download\\\/DD0fDZvD1CUfB2S4TvLy8GgCuGNKqfbuiNLmn6pa895-yK0E6.20FV3zbC9EhWyC\" }] } }"

(反斜杠似乎仅出现在调试器中,所以这不是问题。)

如果我完全这样保留响应,则运行时不会给出任何错误,但是JsonConvert.DeserializeObject不会在C#类的“字段”中返回响应中的数据,否则,如果我删除了REQUESTRESPONSE标头,只留下了INVOICES项,似乎“可行”,但是现在编译器出现了一个新错误:

  

'解析值{时遇到意外字符。路径'VAT_ITEMS',第1行,位置373。'

C#类:

public class GET_INVOICE {
                public string INVOICE_ID             { get; set; }
                public string TYPE                   { get; set; }
                public string CUSTOMER_ID            { get; set; }
                public string CUSTOMER_NUMBER        { get; set; }
                public string CUSTOMER_COSTCENTER_ID { get; set; }
                public string CONTACT_ID             { get; set; }
                public string PROJECT_ID             { get; set; }
                public string CURRENCY_CODE          { get; set; }
                public string DELIVERY_DATE          { get; set; }
                public string INVOICE_TITLE          { get; set; }
                public string CASH_DISCOUNT_PERCENT  { get; set; }
                public string CASH_DISCOUNT_DAYS     { get; set; }
                public string SUB_TOTAL              { get; set; }
                public string VAT_TOTAL              { get; set; }
                public IList<string> VAT_ITEMS       { get; set; }
                public IList<string> ITEMS           { get; set; }
                public double TOTAL                  { get; set; }
                public string ORGANIZATION           { get; set; }
                public string NOTE                   { get; set; }
                public string SALUTATION             { get; set; }
                public string FIRST_NAME             { get; set; }
                public string LAST_NAME              { get; set; }
                public string ADDRESS                { get; set; }
                public string ADDRESS_2              { get; set; }
                public string ZIPCODE                { get; set; }
                public string CITY                   { get; set; }
                public string PAYMENT_TYPE           { get; set; }
                public string BANK_NAME              { get; set; }
                public string BANK_ACCOUNT_NUMBER    { get; set; }
                public string BANK_CODE              { get; set; }
                public string BANK_ACCOUNT_OWNER     { get; set; }
                public string BANK_IBAN              { get; set; }
                public string BANK_BIC               { get; set; }
                public string COUNTRY_CODE           { get; set; }
                public string VAT_ID                 { get; set; }
                public string TEMPLATE_ID            { get; set; }
                public string INVOICE_NUMBER         { get; set; }
                public string INTROTEXT              { get; set; }
                public string PAID_DATE              { get; set; }
                public string IS_CANCELED            { get; set; }
                public string INVOICE_DATE           { get; set; }
                public string DUE_DATE               { get; set; }
                public string PAYMENT_INFO           { get; set; }
                public IList<string> PAYMENTS        { get; set; }
                public string LASTUPDATE             { get; set; }
                public string DOCUMENT_URL           { get; set; }
            }

反序列化

JSON.GET_INVOICE GetInvoiceStruct = JsonConvert.DeserializeObject<JSON.GET_INVOICE>(_response);

我真的不明白如何成功反序列化响应。

1 个答案:

答案 0 :(得分:4)

您已成功为要反序列化的主要对象准备了模型。 现在,您还需要为集合建模其他模型:

  • VAT_ITEMS
  • ITEMS
  • 付款

当前,您已将它们建模为string,但它们根本不是字符串,它们是复杂的对象。 我将为您提供VatItem的示例,然后您可以自己完成其余的工作。

创建VatItem模型,即:

public class VatItem 
{
    //Did not used decimal type because I don't know how JSON.NET handles it
    public double VAT_PERCENT { get; set; }
    public double COMPLETE_NET { get; set; }
    public double VAT_VALUE { get; set; }
}

json响应的这一部分建模:

\"VAT_PERCENT\":\"19.00\",
\"COMPLETE_NET\":22.66,
\"VAT_VALUE\":4.3061344545

然后GET_INVOICE中的更改将您的VAT_ITEMS集合建模为:

public IList<VatItem> VAT_ITEMS { get; set; }

在此示例后面加上两个其他集合以完成映射。