从列表中获取价值(C#UWP) - c#

我有课

 public class RootObject
    {
        public int id { get; set; }
        public int parent_id { get; set; }
        public string status { get; set; }
        public string order_key { get; set; }
        public string currency { get; set; }
        public string version { get; set; }
        public bool prices_include_tax { get; set; }
        public string date_created { get; set; }
        public string date_modified { get; set; }
        public int customer_id { get; set; }
        public double discount_total { get; set; }
        public double discount_tax { get; set; }
        public double shipping_total { get; set; }
        public double shipping_tax { get; set; }
        public double cart_tax { get; set; }
        public double total { get; set; }
        public double total_tax { get; set; }
        public Billing billing { get; set; }
        public Shipping shipping { get; set; }
        public string payment_method { get; set; }
        public string payment_method_title { get; set; }
        public string transaction_id { get; set; }
        public string customer_ip_address { get; set; }
        public string customer_user_agent { get; set; }
        public string created_via { get; set; }
        public string customer_note { get; set; }
        public string date_completed { get; set; }
        public string date_paid { get; set; }
        public string cart_hash { get; set; }
        public List<object> line_items { get; set; }
        public List<object> tax_lines { get; set; }
        public List<object> shipping_lines { get; set; }
        public List<object> fee_lines { get; set; }
        public List<object> coupon_lines { get; set; }

    }

我得到这样的数据

List<RootObject> rootObjectData = JsonConvert.DeserializeObject<List<RootObject>>(products);
   foreach (RootObject root in rootObjectData)
        {
            int id = root.id;
            int customer_id = root.customer_id;

            string fio = root.billing.first_name + " " + "  " + root.billing.last_name;
            string adress = root.billing.address_1 + " " + "  " + root.billing.address_2;
            double total = root.total;
            string telephone = root.billing.phone;

line_items具有List<object>类型

line_items中,我有一些带有数据的json。

例如

"line_items": [
{
  "product_id": 93,
  "quantity": 2
},
{
  "product_id": 22,
  "variation_id": 23,
  "quantity": 1
}

],

我如何获得例如product_id

非常感谢您的帮助!

参考方案

您可以使用非常强大的动态:

List<dynamic> lineItems = jsonObject.line_items;

foreach (dynamic line_item in lineItems)
{
  int productId = line_item.product_id; 
}

请记住:

由于重命名/其他因素,它可能会崩溃-
dynamic非常强大,但是请谨慎使用。

您也可以直接访问它,而无需创建您的“类”-只需反序列化为动态并访问字段即可!

从函数添加driver.get()值时,硒无效参数 - python

更新/解决方案我决定稍微修改一下代码。我最终使用pandas read_csv来打开urls.csv,并使用iterrows()在df列上进行了迭代。现在一切正常。以下是更新的代码段。df = pd.read_csv(urls, header=0, encoding="utf8", index_col=False) for index, …

如何在codeigniter中检查请求是POST还是GET请求? - php

我只是想知道是否有一种非常简单的方法来确定请求是$_POST还是$_GET请求。那Codeigniter有这样的东西吗?$this->container->isGet(); 参考方案 我从未使用过codeigniter,但为此我检查了$_SERVER['REQUEST_METHOD']。看the docs也许像这样:if ($…

对Java接口实现的怀疑 - java

interface Device { public void doIt(); } public class Electronic implements Device { public void doIt() { } } abstract class Phone1 extends Electronic { } abstract class Phone2 ext…

LeetCode题解计算机为什么是基于二进制的?

可以是三进制么?二进制有什么好处?题解:为什么叫电子计算机?算盘应该没有二进制

LeetCode题解统计城市的所有灯泡

这个是我刚毕业的时候,一个真实的面试题,这是一个开放题。题目描述:想办法,将一个城市的所有灯泡数量统计出来。题解:费米估算法1、如果某个城市常驻人口有1000万2、假设每5人居住在一套房里,每套房有灯泡5只,那么住宅灯泡共有1000万只3、假设公众场所每10人共享一只灯泡,那么共有100万只4、主要的这两者相加就得出了1100万只当然实际上这是估算的,具体应…