JSON - 如何根据计数查找子元素列表

JSON - how to find list of child elements based on count

所以我有一些 JSON 数据,我想找到在给定产品节点中多次出现的项目。请参阅下面的示例数据。 例如,"item": "UT9908" 可能在给定的 "line" 数组中出现不止一次。我需要 select 这些项目的列表以及给定 product.line 数组中的项目数量。我正在使用 Linqpad 和我开始使用的代码,至少让我达到了获得产品列表的地步。我真正需要的是在给定 product.line 数组中多次出现的项目列表。我如何获得该列表?

我在下面的代码中得到的是项目及其数量的完整列表。我如何在线阵列中计数?

var jsonPath = @"d:\tempWarehouse_temp_DATA.jsonbak";
        JObject o1 = JObject.Parse(File.ReadAllText(jsonPath));

var deliveries = (from a in o1["order"].Children()["delivery"] select a).ToList();

var product_lines = (from a in deliveries.Children()["product"].Children()["line"] select a).ToList();
//product_lines.Dump();



var items_list = (from a in product_lines.Children()["model_accessory"] 
                    group a by a["item"] into g1 where g1.Count () > 0 
                    select new {item_name = g1.Key, cnt = g1.Count ()} )
                .ToList();                  
items_list.Dump();      





{
  "order": [
    {
      "location": "TTY",
      "osn": "1888TYHHIO",
      "order_type": null,
      "osn_status": "Delivered",
      "order_date": "03012017",
      "customer_name": "234234 test dr",
      "customer_tol_account": null,
      "customer_phone": "234234234",
      "freight_terms": "PREPAID & CHARGE",
      "shipping_instructions": "PH 8747474747",
      "shipping_method": "",
      "additional_service": "",
      "tpoints": "1.9",
      "delivery": [
        {
          "delivery_ad": "",
          "osn_type": "TTU",
          "customer_po_number": "234234",
          "rap": "ONOWR",
          "zip_group": "TYTYY",
          "delivery_date": "04132017",
          "delivery_name": "234 ghhhtol",
          "delivery_address_1": "234 tol fr",
          "delivery_address_2": "031807",
          "delivery_city": "Somewhere",
          "delivery_state": "Idaho",
          "delivery_zip": "111223",
          "delivery_phone": [
            {
              "phone_number": "345345345"
            },
            {
              "phone_number": ""
            }
          ],
          "last_updated_date": "",
          "product": [
            {
              "vendor": "UURL",
              "shipment_number": "",
              "ship_date": "",
              "customer_tracking_number": "",
              "line": [
                {
                  "line_number": "",
                  "line_status": "",
                  "model_accessory": {
                    "crated_indicator": "",
                    "item_type": "M",
                    "item": "TTP99874",
                    "product_type": "",
                    "anti_tip_indicator": "",
                    "product_weight": "",
                    "nmfc": "",
                    "carton_code": "",
                    "quantity": "1"                   
                  }
                },
                {
                  "line_number": "",
                  "line_status": "",
                  "model_accessory": {
                    "crated_indicator": "",
                    "item_type": "A",
                    "item": "UT9908",
                    "product_type": "",
                    "anti_tip_indicator": "",
                    "product_weight": "",
                    "nmfc": "",
                    "carton_code": "",
                    "quantity": "1"                    
                  }
                }
            }
        }
    }
}   

我不确定我是否理解正确,但无论如何: 为此,您必须将 JSON 反序列化为 .NET 对象,然后应用一些 LINQ 查询来获取所需信息。

就像下面这样:

  1. 创建对象(我只是错过了所有需要的字段):

    public class OrderClass
    {
        /* all other properties... */
        public string location { get; set; }
        public DeliveryClass[] delivery { get; set; }
    
    }
    
    public class DeliveryClass
    {
         /* all other properties... */
        public ProductClass[] product { get; set; }
    }
    
    public class ProductClass
    {
         /* all other properties... */
        public LineClass[] line { get; set; }
    
        /* add a method which returns a count of specific lines*/
        public int LineSpecificCount(string s)
        {
            return this.line.Count(l => l.model_accessory.item.Equals(s));
        }
    }
    
    public class LineClass
    {
         /* all other properties... */
        public model_accessoryClass model_accessory { get; set; }
    }
    
    public class model_accessoryClass
    {
         /* all other properties... */
        public string item { get; set; }
        public string item_type { get; set; }
    }
    

然后:

    // get your all orders
    OrderClass[] myOrders = JsonConvert.DeserializeObject<OrderClass[]>(o1["order"].ToString());

    // get orders where item == "UT9908"
    OrderClass[] ordersWithSpecificLine = myOrders.Where(o => o.delivery.Any(d => d.product.Any(p => p.line.Any(l => l.model_accessory.item.Equals("UT9908"))))).ToArray();

    // get all lines in all orders where item == "UT9908"
    LineClass[] lines = myOrders.SelectMany(o => o.delivery.SelectMany(d => d.product.SelectMany(p => p.line.Where(l => l.model_accessory.item.Equals("UT9908"))))).ToArray();

已编辑:

   // get all products
   ProductClass[] myproducts = orders1.SelectMany(o => o.delivery.SelectMany(d => d.product)).ToArray();

   // and now you can access a count something like
   ProductClass[] productsWithSpecificLine = myproducts.Where(p => p.LineSpecificCount("UT9908") > 0).ToArray();

   // productsWithSpecificLine[0].LineSpecificCount("UT9908") == 2
   // productsWithSpecificLine[1].LineSpecificCount("UT9908") == 1

P.S。由于其结构内部存在一些错误,我已经编辑了您的 JSON,并且还添加了额外的行项目:

{
"order": [{
    "location": "TTY",
    "osn": "1888TYHHIO",
    "order_type": null,
    "osn_status": "Delivered",
    "order_date": "03012017",
    "customer_name": "234234 test dr",
    "customer_tol_account": null,
    "customer_phone": "234234234",
    "freight_terms": "PREPAID & CHARGE",
    "shipping_instructions": "PH 8747474747",
    "shipping_method": "",
    "additional_service": "",
    "tpoints": "1.9",
    "delivery": [{
        "delivery_ad": "",
        "osn_type": "TTU",
        "customer_po_number": "234234",
        "rap": "ONOWR",
        "zip_group": "TYTYY",
        "delivery_date": "04132017",
        "delivery_name": "234 ghhhtol",
        "delivery_address_1": "234 tol fr",
        "delivery_address_2": "031807",
        "delivery_city": "Somewhere",
        "delivery_state": "Idaho",
        "delivery_zip": "111223",
        "delivery_phone": [{
            "phone_number": "345345345"
        },
        {
            "phone_number": ""
        }],
        "last_updated_date": "",
        "product": [{
            "vendor": "UURL",
            "shipment_number": "",
            "ship_date": "",
            "customer_tracking_number": "",
            "line": [{
                "line_number": "",
                "line_status": "",
                "model_accessory": {
                    "crated_indicator": "",
                    "item_type": "M",
                    "item": "TTP99874",
                    "product_type": "",
                    "anti_tip_indicator": "",
                    "product_weight": "",
                    "nmfc": "",
                    "carton_code": "",
                    "quantity": "1"
                }
            },
            {
                "line_number": "",
                "line_status": "",
                "model_accessory": {
                    "crated_indicator": "",
                    "item_type": "A",
                    "item": "UT9908",
                    "product_type": "",
                    "anti_tip_indicator": "",
                    "product_weight": "",
                    "nmfc": "",
                    "carton_code": "",
                    "quantity": "1"
                }
            },
            {
                "line_number": "",
                "line_status": "",
                "model_accessory": {
                    "crated_indicator": "",
                    "item_type": "B",
                    "item": "UT9908",
                    "product_type": "",
                    "anti_tip_indicator": "",
                    "product_weight": "",
                    "nmfc": "",
                    "carton_code": "",
                    "quantity": "1"
                }
            }]
        },
        {
            "vendor": "UURL",
            "shipment_number": "",
            "ship_date": "",
            "customer_tracking_number": "",
            "line": [{
                "line_number": "",
                "line_status": "",
                "model_accessory": {
                    "crated_indicator": "",
                    "item_type": "Z",
                    "item": "TTP99874",
                    "product_type": "",
                    "anti_tip_indicator": "",
                    "product_weight": "",
                    "nmfc": "",
                    "carton_code": "",
                    "quantity": "1"
                }
            },
            {
                "line_number": "",
                "line_status": "",
                "model_accessory": {
                    "crated_indicator": "",
                    "item_type": "X",
                    "item": "UT9908",
                    "product_type": "",
                    "anti_tip_indicator": "",
                    "product_weight": "",
                    "nmfc": "",
                    "carton_code": "",
                    "quantity": "1"
                }
            },
            {
                "line_number": "",
                "line_status": "",
                "model_accessory": {
                    "crated_indicator": "",
                    "item_type": "Y",
                    "item": "UT9909",
                    "product_type": "",
                    "anti_tip_indicator": "",
                    "product_weight": "",
                    "nmfc": "",
                    "carton_code": "",
                    "quantity": "1"
                }
            }]
        }]
    }]
}]
}