【问题标题】:Get value from List (C# UWP)从列表中获取值(C# UWP)
【发布时间】:2017-09-30 01:23:36
【问题描述】:

我有课

 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_itemsList&lt;object&gt; 类型

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

例如

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

],

我如何获得例如product_id

非常感谢您的帮助!

【问题讨论】:

  • 您有搜索条件吗?如果是这样,您可以使用 linq 表达式搜索列表,然后使用 Object.Value 提取值
  • 是的,我知道我需要哪些字段。如何搜索和提取? @Takarii
  • 快速查看this page on JSON queries with linq,看看它是否满足您的需求
  • 看。我有 JSON,有课程并且有非现实化。我需要去列出并从中提取我需要的数据。就像我对id 所做的那样,我编辑了帖子@Takarii
  • 所以,使用我发布的链接,用 linq 构造查询以获取包含该值的对象,然后直接访问该值。

标签: c# uwp


【解决方案1】:

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

List<dynamic> lineItems = jsonObject.line_items;

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

请记住:

它可能由于重命名/其他因素而崩溃 - dynamic 很强大,但要谨慎使用。

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

【讨论】:

    【解决方案2】:

    如果不够明显

    using Newtonsoft.Json.Linq;
    
    foreach(object line_item in line_items)
    {
        JObject line_item_jobject = line_item as JObject;
        Jvalue product_id = line_item_jobject["product_id"] as JValue;
        int product_id_value = (int)product_id.Value;
    
        // Another way
        int product_id_value2 = line_item_jobject.Value<int>("product_id");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-27
      • 1970-01-01
      • 2014-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-15
      相关资源
      最近更新 更多