如何反序列化json数组C# [英] How to deserialize json array C#

查看:78
本文介绍了如何反序列化json数组C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想反序列化以下数组



I want to De-serialize the following array

[
  {
    "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
  }
]







我想打印所有数组分别在单独的网格视图中。目前在网格视图中,但之后我将在水晶报告中使用这些数据,所以如果可能请建议直接使用数据到水晶报告的方式。



我尝试过:



我试过了






I want to print all the arrays separately in seperate grid views. Currently in grid view but then after i will be using this data in crystal reports, so if possible please suggest way to direct use the data to crystal reports as well.

What I have tried:

I have tried

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





显示空白数据。



It shows the blank data.

推荐答案

我根据本论坛中提出的关于将JSON数据反序列化到类的常见问题撰写了一篇文章。它将回答这个问题以及更多。



你可以在这里找到这篇文章:在C#中使用JSON& VB [ ^ ]



许多新的反序列化JSON的常见错误是忽略了键区分大小写的事实,所以你需要匹配属性名称或描述命名键的JsonProperty属性。



避免这些错误的最简单方法是使用第三方工具,例如JSONUtils [ ^ ]生成课程(es)来自原始JSON。例如:

I've written an article based on common questions asked in this forum regarding deserialization of JSON data to classes. It will answer this question and much more.

You can find the article here: Working with JSON in C# & VB[^]

A common mistake that many make that are new to deserializing JSON is ignoring the fact that keys are case sensitive, so you need to match either with the property name or a JsonProperty attribute describing the named key.

The easiest way to avoid these mistakes is to use 3rd party tools, like JSONUtils[^] to generate the class(es) for you from the raw JSON. For example:
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 OrderTax { get; set; }

	[JsonProperty("total_tax")]
	public double TotalTax { get; set; }

	[JsonProperty("shipping")]
	public int Shipping { get; set; }

	[JsonProperty("grand_total")]
	public double GrandTotal { get; set; }

	[JsonProperty("sale_status")]
	public string SaleStatus { get; set; }

	[JsonProperty("payment_status")]
	public string PaymentStatus { get; set; }

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

	[JsonProperty("due_date")]
	public object DueDate { get; set; }

	[JsonProperty("created_by")]
	public int CreatedBy { get; set; }

	[JsonProperty("updated_by")]
	public object UpdatedBy { get; set; }

	[JsonProperty("updated_at")]
	public object UpdatedAt { get; set; }

	[JsonProperty("total_items")]
	public int TotalItems { get; set; }

	[JsonProperty("pos")]
	public int Pos { get; set; }

	[JsonProperty("paid")]
	public int Paid { get; set; }

	[JsonProperty("return_id")]
	public object ReturnId { get; set; }

	[JsonProperty("surcharge")]
	public int Surcharge { get; set; }

	[JsonProperty("attachment")]
	public object Attachment { get; set; }

	[JsonProperty("return_sale_ref")]
	public object ReturnSaleRef { get; set; }

	[JsonProperty("sale_id")]
	public object SaleId { get; set; }

	[JsonProperty("return_sale_total")]
	public int ReturnSaleTotal { get; set; }

	[JsonProperty("rounding")]
	public object Rounding { get; set; }

	[JsonProperty("suspend_note")]
	public object SuspendNote { get; set; }

	[JsonProperty("api")]
	public int Api { get; set; }

	[JsonProperty("shop")]
	public int Shop { get; set; }

	[JsonProperty("address_id")]
	public object AddressId { get; set; }

	[JsonProperty("reserve_id")]
	public object ReserveId { get; set; }

	[JsonProperty("hash")]
	public string Hash { get; set; }

	[JsonProperty("cgst")]
	public double Cgst { get; set; }

	[JsonProperty("sgst")]
	public double Sgst { get; set; }

	[JsonProperty("igst")]
	public int Igst { get; set; }

	[JsonProperty("purchase_order_number")]
	public string PurchaseOrderNumber { get; set; }

	[JsonProperty("vehicle_number")]
	public string VehicleNumber { get; set; }

	[JsonProperty("challan_number")]
	public string ChallanNumber { get; set; }

	[JsonProperty("site_address")]
	public string SiteAddress { get; set; }

	[JsonProperty("tax_method")]
	public int TaxMethod { get; set; }

	[JsonProperty("sale_type")]
	public int SaleType { get; set; }

	[JsonProperty("sattlementAmount")]
	public int SattlementAmount { get; set; }
}

public class Row
{

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

	[JsonProperty("sale_id")]
	public int SaleId { get; set; }

	[JsonProperty("product_id")]
	public int ProductId { get; set; }

	[JsonProperty("product_code")]
	public int ProductCode { get; set; }

	[JsonProperty("product_name")]
	public string ProductName { get; set; }

	[JsonProperty("product_type")]
	public string ProductType { get; set; }

	[JsonProperty("option_id")]
	public object OptionId { get; set; }

	[JsonProperty("net_unit_price")]
	public int NetUnitPrice { get; set; }

	[JsonProperty("unit_price")]
	public double UnitPrice { get; set; }

	[JsonProperty("quantity")]
	public int Quantity { get; set; }

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

	[JsonProperty("item_tax")]
	public double ItemTax { get; set; }

	[JsonProperty("tax_rate_id")]
	public int TaxRateId { get; set; }

	[JsonProperty("tax")]
	public string Tax { get; set; }

	[JsonProperty("discount")]
	public int Discount { get; set; }

	[JsonProperty("item_discount")]
	public int ItemDiscount { get; set; }

	[JsonProperty("subtotal")]
	public double Subtotal { get; set; }

	[JsonProperty("serial_no")]
	public string SerialNo { get; set; }

	[JsonProperty("real_unit_price")]
	public int RealUnitPrice { get; set; }

	[JsonProperty("sale_item_id")]
	public object SaleItemId { get; set; }

	[JsonProperty("product_unit_id")]
	public int ProductUnitId { get; set; }

	[JsonProperty("product_unit_code")]
	public string ProductUnitCode { get; set; }

	[JsonProperty("unit_quantity")]
	public int UnitQuantity { get; set; }

	[JsonProperty("comment")]
	public object Comment { get; set; }

	[JsonProperty("gst")]
	public string Gst { get; set; }

	[JsonProperty("cgst")]
	public double Cgst { get; set; }

	[JsonProperty("sgst")]
	public double Sgst { get; set; }

	[JsonProperty("igst")]
	public int Igst { get; set; }

	[JsonProperty("tax_code")]
	public string TaxCode { get; set; }

	[JsonProperty("tax_name")]
	public string TaxName { get; set; }

	[JsonProperty("tax_rate")]
	public int TaxRate { get; set; }

	[JsonProperty("image")]
	public string Image { get; set; }

	[JsonProperty("details")]
	public string Details { get; set; }

	[JsonProperty("variant")]
	public object Variant { get; set; }

	[JsonProperty("hsn_code")]
	public int HsnCode { get; set; }
}

public class Example
{

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

	[JsonProperty("payments")]
	public bool Payments { get; set; }

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

	[JsonProperty("user")]
	public User User { get; set; }

	[JsonProperty("inv")]
	public Inv Inv { get; set; }

	[JsonProperty("rows")]
	public IList<Row> Rows { get; set; }

	[JsonProperty("return_sale")]
	public object ReturnSale { get; set; }

	[JsonProperty("return_rows")]
	public object ReturnRows { get; set; }
}



The article link above will explain in more detail the rest of the answer for you.


The article link above will explain in more detail the rest of the answer for you.


这篇关于如何反序列化json数组C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆