JSON中对象的计数(UWP c#)

本文关键字:UWP 对象 JSON | 更新日期: 2023-09-27 18:05:57

我正在为Windows 10编写应用程序。

我使用WooCommerce。. Net插件从WorsPress站点下载数据。

我是这样下载的。

 RestAPI rest = new RestAPI("http://simplegames.com.ua/wp-json/wc/v1/", "ck_9d64c027d2c5f81b8bed3342eeccc6d337be813d", "cs_60697b1e6cbdeb8d62d19e0765e339f8e3334754");
        WCObject wc = new WCObject(rest);
        //Get all products
        var orders = await wc.GetOrders();
var  products = orders[0].line_items;
{
  "id": 1897,
  "parent_id": 0,
  "status": "processing",
  "order_key": "wc_order_57cec87491975",
  "currency": "UAH",
  "version": "2.6.4",
  "prices_include_tax": false,
  "date_created": "2016-09-06T13:45:24",
  "date_modified": "2016-09-06T13:45:24",
  "customer_id": 0,
  "discount_total": 0.00,
  "discount_tax": 0.00,
  "shipping_total": 0.00,
  "shipping_tax": 0.00,
  "cart_tax": 0.00,
  "total": 140.00,
  "total_tax": 0.00,
  "billing": {
    "first_name": "Евгений",
    "last_name": "Сухомлин",
    "company": "",
    "address_1": "sdfsdf",
    "address_2": "sdfsdfsdfdf",
    "city": "",
    "state": "",
    "postcode": "",
    "country": "",
    "email": "nemesises@live.com",
    "phone": "3904354345"
  },
  "shipping": {
    "first_name": "Евгений",
    "last_name": "Сухомлин",
    "company": "",
    "address_1": "sdfsdf",
    "address_2": "sdfsdfsdfdf",
    "city": "",
    "state": "",
    "postcode": "",
    "country": ""
  },
  "payment_method": "cod",
  "payment_method_title": "Оплата готівкою",
  "transaction_id": "",
  "customer_ip_address": "46.133.88.220",
  "customer_user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36",
  "created_via": "checkout",
  "customer_note": "",
  "date_completed": "2016-09-06T16:45:24",
  "date_paid": "",
  "cart_hash": "187385bf4fb75116424e9cfeb1aa9ccd",
  "line_items": [
    {
      "id": 75,
      "name": "Сформуй сам",
      "sku": "",
      "product_id": 1271,
      "variation_id": 0,
      "quantity": 1,
      "tax_class": "",
      "price": 40.00,
      "subtotal": 40.00,
      "subtotal_tax": 0.00,
      "total": 40.00,
      "total_tax": 0.00,
      "taxes": [
      ],
      "meta": [
        {
          "key": "Розмір піци (обов'язково) <span class='"woocommerce-Price-amount amount'">40&nbsp;<span class='"woocommerce-Price-currencySymbol'">грн</span></span>",
          "label": "Розмір піци (обов'язково) <span class='"woocommerce Price amount amount'">40&nbsp;<span class='"woocommerce Price currencySymbol'">грн</span></span>",
          "value": "Піца база велика (380г)"
        }
      ]
    },
    {
      "id": 76,
      "name": "Сформуй сам",
      "sku": "",
      "product_id": 1271,
      "variation_id": 0,
      "quantity": 1,
      "tax_class": "",
      "price": 100.00,
      "subtotal": 100.00,
      "subtotal_tax": 0.00,
      "total": 100.00,
      "total_tax": 0.00,
      "taxes": [
      ],
      "meta": [
        {
          "key": "Розмір піци (обов'язково) <span class='"woocommerce-Price-amount amount'">30&nbsp;<span class='"woocommerce-Price-currencySymbol'">грн</span></span>",
          "label": "Розмір піци (обов'язково) <span class='"woocommerce Price amount amount'">30&nbsp;<span class='"woocommerce Price currencySymbol'">грн</span></span>",
          "value": "Піца база мала (280г)"
        },
        {
          "key": "Ананас (9грн) <span class='"woocommerce-Price-amount amount'">9&nbsp;<span class='"woocommerce-Price-currencySymbol'">грн</span></span>",
          "label": "Ананас (9грн) <span class='"woocommerce Price amount amount'">9&nbsp;<span class='"woocommerce Price currencySymbol'">грн</span></span>",
          "value": "1"
        },
        {
          "key": "Балик (11грн) <span class='"woocommerce-Price-amount amount'">11&nbsp;<span class='"woocommerce-Price-currencySymbol'">грн</span></span>",
          "label": "Балик (11грн) <span class='"woocommerce Price amount amount'">11&nbsp;<span class='"woocommerce Price currencySymbol'">грн</span></span>",
          "value": "1"
        },
        {
          "key": "Бекон (9грн) <span class='"woocommerce-Price-amount amount'">9&nbsp;<span class='"woocommerce-Price-currencySymbol'">грн</span></span>",
          "label": "Бекон (9грн) <span class='"woocommerce Price amount amount'">9&nbsp;<span class='"woocommerce Price currencySymbol'">грн</span></span>",
          "value": "1"
        },
        {
          "key": "Свинина вялена (12грн) <span class='"woocommerce-Price-amount amount'">12&nbsp;<span class='"woocommerce-Price-currencySymbol'">грн</span></span>",
          "label": "Свинина вялена (12грн) <span class='"woocommerce Price amount amount'">12&nbsp;<span class='"woocommerce Price currencySymbol'">грн</span></span>",
          "value": "1"
        },
        {
          "key": "Зелень (4грн) <span class='"woocommerce-Price-amount amount'">4&nbsp;<span class='"woocommerce-Price-currencySymbol'">грн</span></span>",
          "label": "Зелень (4грн) <span class='"woocommerce Price amount amount'">4&nbsp;<span class='"woocommerce Price currencySymbol'">грн</span></span>",
          "value": "1"
        },
        {
          "key": "Копчене куряче філе (10грн) <span class='"woocommerce-Price-amount amount'">10&nbsp;<span class='"woocommerce-Price-currencySymbol'">грн</span></span>",
          "label": "Копчене куряче філе (10грн) <span class='"woocommerce Price amount amount'">10&nbsp;<span class='"woocommerce Price currencySymbol'">грн</span></span>",
          "value": "1"
        },
        {
          "key": "Крабові палички (11грн) <span class='"woocommerce-Price-amount amount'">11&nbsp;<span class='"woocommerce-Price-currencySymbol'">грн</span></span>",
          "label": "Крабові палички (11грн) <span class='"woocommerce Price amount amount'">11&nbsp;<span class='"woocommerce Price currencySymbol'">грн</span></span>",
          "value": "1"
        },
        {
          "key": "Кукурудза консервована (4грн) <span class='"woocommerce-Price-amount amount'">4&nbsp;<span class='"woocommerce-Price-currencySymbol'">грн</span></span>",
          "label": "Кукурудза консервована (4грн) <span class='"woocommerce Price amount amount'">4&nbsp;<span class='"woocommerce Price currencySymbol'">грн</span></span>",
          "value": "1"
        }
      ]
    }
  ],
  "tax_lines": [
  ],
  "shipping_lines": [
    {
      "id": 77,
      "method_title": "Безкоштовна доставка",
      "method_id": "free_shipping:5",
      "total": 0.00,
      "total_tax": 0.00,
      "taxes": [
      ]
    }
  ],
  "fee_lines": [
  ],
  "coupon_lines": [
  ]
}

在line_items中我可以有多个对象

我尝试使用JSON。. NET插件从Newtosoft,但这不起作用。

JArray items = (JArray)test["JSONObject"];
int length = items.Count;
for (int i = 0; i < items.Count; i++)
{
    var item = (JObject)items[i];
    //do something with item
}`

如何计数它们并在控制台显示,例如每个name字段?

JSON中对象的计数(UWP c#)

我解决了问题。

有问题的代码

 JArray jObj = (JArray)JsonConvert.DeserializeObject(products);
            int count = jObj.Count;
            Debug.WriteLine(count);