Naikniket Ответов: 1

Как десериализовать массив json C#


Я хочу де-сериализовать следующий массив

[
  {
    "customer": {
      "id": 1,
      "group_id": 3,
      "group_name": "customer",
      "customer_group_id": 1,
      "customer_group_name": "General",
      "name": "Walk-in Customer",
      "company": "Walk-in Customer",
      "vat_no": "",
      "address": "Customer Address",
      "city": "",
      "state": "",
      "postal_code": ,
      "country": "India",
      "phone": 123456789,
      "email": "",
      "cf1": "",
      "cf2": "",
      "cf3": "",
      "cf4": "",
      "cf5": "",
      "cf6": "",
      "invoice_footer": null,
      "payment_term": 0,
      "logo": "logo.png",
      "award_points": 0,
      "deposit_amount": null,
      "price_group_id": null,
      "price_group_name": null,
      "account_name": "",
      "opbal": 0,
      "opbal_type": "Dr",
      "cheque_printing_name": "",
      "print_name": "",
      "bank_name": "",
      "party_type": "",
      "group_account": "",
      "bank_account_number": "",
      "gst_no": "",
      "birthday_on": "0000-00-00",
      "aniversary_no": "0000-00-00"
    },
    "payments": false,
    "biller": {
      "id": 3,
      "group_id": null,
      "group_name": "biller",
      "customer_group_id": null,
      "customer_group_name": null,
      "name": "Garden Art",
      "company": "",
      "vat_no": "",
      "address": "",
      "city": "",
      "state": "",
      "postal_code": ,
      "country": "",
      "phone": ,
      "email": "",
      "cf1": "",
      "cf2": "",
      "cf3": "",
      "cf4": "",
      "cf5": "",
      "cf6": "",
      "invoice_footer": " Thank you for shopping with us. Please come again",
      "payment_term": 0,
      "logo": "",
      "award_points": 0,
      "deposit_amount": null,
      "price_group_id": null,
      "price_group_name": null,
      "account_name": "",
      "opbal": "",
      "opbal_type": "",
      "cheque_printing_name": "",
      "print_name": "",
      "bank_name": "",
      "party_type": "",
      "group_account": "",
      "bank_account_number": "",
      "gst_no": "",
      "birthday_on": "0000-00-00",
      "aniversary_no": "0000-00-00"
    },
    "user": {
      "id": 1,
      "last_ip_address": "::1",
      "ip_address": "\u0000\u0000",
      "username": "",
      "password": "",
      "salt": null,
      "email": "",
      "activation_code": null,
      "forgotten_password_code": null,
      "forgotten_password_time": null,
      "remember_code": "",
      "created_on": ,
      "last_login": ,
      "active": 1,
      "first_name": "",
      "last_name": "",
      "company": "",
      "phone": "",
      "avatar": null,
      "gender": "female",
      "group_id": 1,
      "warehouse_id": null,
      "biller_id": null,
      "company_id": null,
      "show_cost": 0,
      "show_price": 0,
      "award_points": 0,
      "view_right": 0,
      "edit_right": 0,
      "allow_discount": 0
    },
    "inv": {
      "id": 744,
      "date": "2019-03-02 21:32:00",
      "reference_no": "",
      "customer_id": 1,
      "customer": "Walk-in Customer",
      "biller_id": 3,
      "biller": "",
      "warehouse_id": 3,
      "note": "",
      "staff_note": "",
      "total": 30,
      "product_discount": 0,
      "order_discount_id": "",
      "total_discount": 0,
      "order_discount": 0,
      "product_tax": 5.4,
      "order_tax_id": 3,
      "order_tax": 2.124,
      "total_tax": 7.524,
      "shipping": 0,
      "grand_total": 37.524,
      "sale_status": "completed",
      "payment_status": "pending",
      "payment_term": 0,
      "due_date": null,
      "created_by": 1,
      "updated_by": null,
      "updated_at": null,
      "total_items": 2,
      "pos": 0,
      "paid": 0,
      "return_id": null,
      "surcharge": 0,
      "attachment": null,
      "return_sale_ref": null,
      "sale_id": null,
      "return_sale_total": 0,
      "rounding": null,
      "suspend_note": null,
      "api": 0,
      "shop": 0,
      "address_id": null,
      "reserve_id": null,
      "hash": "",
      "cgst": 2.7,
      "sgst": 2.7,
      "igst": 0,
      "purchase_order_number": "",
      "vehicle_number": "",
      "challan_number": "",
      "site_address": "",
      "tax_method": 1,
      "sale_type": 2,
      "sattlementAmount": 0
    },
    "rows": [
      {
        "id": 8305,
        "sale_id": 744,
        "product_id": 1057,
        "product_code": 13780471,
        "product_name": "Magiee Noodles 20rs",
        "product_type": "standard",
        "option_id": null,
        "net_unit_price": 20,
        "unit_price": 23.6,
        "quantity": 1,
        "warehouse_id": 3,
        "item_tax": 3.6,
        "tax_rate_id": 8,
        "tax": "18%",
        "discount": 0,
        "item_discount": 0,
        "subtotal": 23.6,
        "serial_no": "",
        "real_unit_price": 20,
        "sale_item_id": null,
        "product_unit_id": 22,
        "product_unit_code": "Nos",
        "unit_quantity": 1,
        "comment": null,
        "gst": "9%",
        "cgst": 1.8,
        "sgst": 1.8,
        "igst": 0,
        "tax_code": "GST@18%",
        "tax_name": "GST@18%",
        "tax_rate": 18,
        "image": "no_image.png",
        "details": "",
        "variant": null,
        "hsn_code": 123341212
      },
      {
        "id": 8306,
        "sale_id": 744,
        "product_id": 1058,
        "product_code": 22508494,
        "product_name": "GoodDay 10rs",
        "product_type": "standard",
        "option_id": null,
        "net_unit_price": 10,
        "unit_price": 11.8,
        "quantity": 1,
        "warehouse_id": 3,
        "item_tax": 1.8,
        "tax_rate_id": 8,
        "tax": "18%",
        "discount": 0,
        "item_discount": 0,
        "subtotal": 11.8,
        "serial_no": "",
        "real_unit_price": 10,
        "sale_item_id": null,
        "product_unit_id": 22,
        "product_unit_code": "Nos",
        "unit_quantity": 1,
        "comment": null,
        "gst": "9%",
        "cgst": 0.9,
        "sgst": 0.9,
        "igst": 0,
        "tax_code": "GST@18%",
        "tax_name": "GST@18%",
        "tax_rate": 18,
        "image": "no_image.png",
        "details": "",
        "variant": null,
        "hsn_code": 1233412
      }
    ],
    "return_sale": null,
    "return_rows": null
  }
]



Я хочу распечатать все массивы отдельно в отдельных видах сетки. В настоящее время в виде сетки, но затем после того, как я буду использовать эти данные в crystal reports, поэтому, если это возможно, пожалуйста, предложите способ прямого использования данных в crystal reports.

Что я уже пробовал:

Я уже пробовал

WebClient wc = new WebClient();
var json = wc.DownloadString("url");
List<User> users = JsonConvert.DeserializeObject<List<User>>(json);
dataGridView1.DataSource = users;


Он показывает пустые данные.

1 Ответов

Рейтинг:
12

Graeme_Grant

Я написал статью, основанную на общих вопросах, задаваемых на этом форуме относительно десериализации данных JSON в классы. Она ответит на этот вопрос и многое другое.

Вы можете найти эту статью здесь: Работа с JSON в C# и VB[^]

Распространенная ошибка, которую совершают многие новички в десериализации JSON, заключается в игнорировании того факта, что ключи чувствительны к регистру, поэтому вам нужно сопоставить либо имя свойства, либо атрибут JsonProperty, описывающий именованный ключ.

Самый простой способ избежать этих ошибок-использовать сторонние инструменты, такие как JSONUtils[^] чтобы сгенерировать класс(ы) для вас из необработанного JSON. Например:

public class Customer
{

	[JsonProperty("id")]
	public int Id { get; set; }

	[JsonProperty("group_id")]
	public int GroupId { get; set; }

	[JsonProperty("group_name")]
	public string GroupName { get; set; }

	[JsonProperty("customer_group_id")]
	public int CustomerGroupId { get; set; }

	[JsonProperty("customer_group_name")]
	public string CustomerGroupName { get; set; }

	[JsonProperty("name")]
	public string Name { get; set; }

	[JsonProperty("company")]
	public string Company { get; set; }

	[JsonProperty("vat_no")]
	public string VatNo { get; set; }

	[JsonProperty("address")]
	public string Address { get; set; }

	[JsonProperty("city")]
	public string City { get; set; }

	[JsonProperty("state")]
	public string State { get; set; }

	[JsonProperty("postal_code")]
	public object PostalCode { get; set; }

	[JsonProperty("country")]
	public string Country { get; set; }

	[JsonProperty("phone")]
	public int Phone { get; set; }

	[JsonProperty("email")]
	public string Email { get; set; }

	[JsonProperty("cf1")]
	public string Cf1 { get; set; }

	[JsonProperty("cf2")]
	public string Cf2 { get; set; }

	[JsonProperty("cf3")]
	public string Cf3 { get; set; }

	[JsonProperty("cf4")]
	public string Cf4 { get; set; }

	[JsonProperty("cf5")]
	public string Cf5 { get; set; }

	[JsonProperty("cf6")]
	public string Cf6 { get; set; }

	[JsonProperty("invoice_footer")]
	public object InvoiceFooter { get; set; }

	[JsonProperty("payment_term")]
	public int PaymentTerm { get; set; }

	[JsonProperty("logo")]
	public string Logo { get; set; }

	[JsonProperty("award_points")]
	public int AwardPoints { get; set; }

	[JsonProperty("deposit_amount")]
	public object DepositAmount { get; set; }

	[JsonProperty("price_group_id")]
	public object PriceGroupId { get; set; }

	[JsonProperty("price_group_name")]
	public object PriceGroupName { get; set; }

	[JsonProperty("account_name")]
	public string AccountName { get; set; }

	[JsonProperty("opbal")]
	public int Opbal { get; set; }

	[JsonProperty("opbal_type")]
	public string OpbalType { get; set; }

	[JsonProperty("cheque_printing_name")]
	public string ChequePrintingName { get; set; }

	[JsonProperty("print_name")]
	public string PrintName { get; set; }

	[JsonProperty("bank_name")]
	public string BankName { get; set; }

	[JsonProperty("party_type")]
	public string PartyType { get; set; }

	[JsonProperty("group_account")]
	public string GroupAccount { get; set; }

	[JsonProperty("bank_account_number")]
	public string BankAccountNumber { get; set; }

	[JsonProperty("gst_no")]
	public string GstNo { get; set; }

	[JsonProperty("birthday_on")]
	public string BirthdayOn { get; set; }

	[JsonProperty("aniversary_no")]
	public string AniversaryNo { get; set; }
}

public class Biller
{

	[JsonProperty("id")]
	public int Id { get; set; }

	[JsonProperty("group_id")]
	public object GroupId { get; set; }

	[JsonProperty("group_name")]
	public string GroupName { get; set; }

	[JsonProperty("customer_group_id")]
	public object CustomerGroupId { get; set; }

	[JsonProperty("customer_group_name")]
	public object CustomerGroupName { get; set; }

	[JsonProperty("name")]
	public string Name { get; set; }

	[JsonProperty("company")]
	public string Company { get; set; }

	[JsonProperty("vat_no")]
	public string VatNo { get; set; }

	[JsonProperty("address")]
	public string Address { get; set; }

	[JsonProperty("city")]
	public string City { get; set; }

	[JsonProperty("state")]
	public string State { get; set; }

	[JsonProperty("postal_code")]
	public object PostalCode { get; set; }

	[JsonProperty("country")]
	public string Country { get; set; }

	[JsonProperty("phone")]
	public object Phone { get; set; }

	[JsonProperty("email")]
	public string Email { get; set; }

	[JsonProperty("cf1")]
	public string Cf1 { get; set; }

	[JsonProperty("cf2")]
	public string Cf2 { get; set; }

	[JsonProperty("cf3")]
	public string Cf3 { get; set; }

	[JsonProperty("cf4")]
	public string Cf4 { get; set; }

	[JsonProperty("cf5")]
	public string Cf5 { get; set; }

	[JsonProperty("cf6")]
	public string Cf6 { get; set; }

	[JsonProperty("invoice_footer")]
	public string InvoiceFooter { get; set; }

	[JsonProperty("payment_term")]
	public int PaymentTerm { get; set; }

	[JsonProperty("logo")]
	public string Logo { get; set; }

	[JsonProperty("award_points")]
	public int AwardPoints { get; set; }

	[JsonProperty("deposit_amount")]
	public object DepositAmount { get; set; }

	[JsonProperty("price_group_id")]
	public object PriceGroupId { get; set; }

	[JsonProperty("price_group_name")]
	public object PriceGroupName { get; set; }

	[JsonProperty("account_name")]
	public string AccountName { get; set; }

	[JsonProperty("opbal")]
	public string Opbal { get; set; }

	[JsonProperty("opbal_type")]
	public string OpbalType { get; set; }

	[JsonProperty("cheque_printing_name")]
	public string ChequePrintingName { get; set; }

	[JsonProperty("print_name")]
	public string PrintName { get; set; }

	[JsonProperty("bank_name")]
	public string BankName { get; set; }

	[JsonProperty("party_type")]
	public string PartyType { get; set; }

	[JsonProperty("group_account")]
	public string GroupAccount { get; set; }

	[JsonProperty("bank_account_number")]
	public string BankAccountNumber { get; set; }

	[JsonProperty("gst_no")]
	public string GstNo { get; set; }

	[JsonProperty("birthday_on")]
	public string BirthdayOn { get; set; }

	[JsonProperty("aniversary_no")]
	public string AniversaryNo { get; set; }
}

public class User
{

	[JsonProperty("id")]
	public int Id { get; set; }

	[JsonProperty("last_ip_address")]
	public string LastIpAddress { get; set; }

	[JsonProperty("ip_address")]
	public string IpAddress { get; set; }

	[JsonProperty("username")]
	public string Username { get; set; }

	[JsonProperty("password")]
	public string Password { get; set; }

	[JsonProperty("salt")]
	public object Salt { get; set; }

	[JsonProperty("email")]
	public string Email { get; set; }

	[JsonProperty("activation_code")]
	public object ActivationCode { get; set; }

	[JsonProperty("forgotten_password_code")]
	public object ForgottenPasswordCode { get; set; }

	[JsonProperty("forgotten_password_time")]
	public object ForgottenPasswordTime { get; set; }

	[JsonProperty("remember_code")]
	public string RememberCode { get; set; }

	[JsonProperty("created_on")]
	public object CreatedOn { get; set; }

	[JsonProperty("last_login")]
	public object LastLogin { get; set; }

	[JsonProperty("active")]
	public int Active { get; set; }

	[JsonProperty("first_name")]
	public string FirstName { get; set; }

	[JsonProperty("last_name")]
	public string LastName { get; set; }

	[JsonProperty("company")]
	public string Company { get; set; }

	[JsonProperty("phone")]
	public string Phone { get; set; }

	[JsonProperty("avatar")]
	public object Avatar { get; set; }

	[JsonProperty("gender")]
	public string Gender { get; set; }

	[JsonProperty("group_id")]
	public int GroupId { get; set; }

	[JsonProperty("warehouse_id")]
	public object WarehouseId { get; set; }

	[JsonProperty("biller_id")]
	public object BillerId { get; set; }

	[JsonProperty("company_id")]
	public object CompanyId { get; set; }

	[JsonProperty("show_cost")]
	public int ShowCost { get; set; }

	[JsonProperty("show_price")]
	public int ShowPrice { get; set; }

	[JsonProperty("award_points")]
	public int AwardPoints { get; set; }

	[JsonProperty("view_right")]
	public int ViewRight { get; set; }

	[JsonProperty("edit_right")]
	public int EditRight { get; set; }

	[JsonProperty("allow_discount")]
	public int AllowDiscount { get; set; }
}

public class Inv
{

	[JsonProperty("id")]
	public int Id { get; set; }

	[JsonProperty("date")]
	public string Date { get; set; }

	[JsonProperty("reference_no")]
	public string ReferenceNo { get; set; }

	[JsonProperty("customer_id")]
	public int CustomerId { get; set; }

	[JsonProperty("customer")]
	public string Customer { get; set; }

	[JsonProperty("biller_id")]
	public int BillerId { get; set; }

	[JsonProperty("biller")]
	public string Biller { get; set; }

	[JsonProperty("warehouse_id")]
	public int WarehouseId { get; set; }

	[JsonProperty("note")]
	public string Note { get; set; }

	[JsonProperty("staff_note")]
	public string StaffNote { get; set; }

	[JsonProperty("total")]
	public int Total { get; set; }

	[JsonProperty("product_discount")]
	public int ProductDiscount { get; set; }

	[JsonProperty("order_discount_id")]
	public string OrderDiscountId { get; set; }

	[JsonProperty("total_discount")]
	public int TotalDiscount { get; set; }

	[JsonProperty("order_discount")]
	public int OrderDiscount { get; set; }

	[JsonProperty("product_tax")]
	public double ProductTax { get; set; }

	[JsonProperty("order_tax_id")]
	public int OrderTaxId { get; set; }

	[JsonProperty("order_tax")]
	public double