所以我有一些JSON数据,我想找到在给定产品节点中出现多次的项目。请参阅下面的示例数据 例如,“item”:“UT9908”可能在给定的“line”数组中出现不止一次。我需要选择这些项目的列表以及给定product.line数组中的项目数量。我使用Linqpad和代码Im开头,至少让我到了获得产品列表的地步。我真正需要的是在给定的product.line数组中出现多次的项目列表。我该如何获得该清单?
我在下面的代码中得到的是项目及其计数的总体列表。我如何计算线阵列中的内容?
var jsonPath = @"d:\temp\1Warehouse_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"
}
}
}
}
}
}
答案 0 :(得分:1)
我不确定我是否理解,但无论如何: 为此,您必须将JSON反序列化为.NET对象,然后应用一些LINQ查询来获取所需信息。
如下所示:
创建对象(我只是错过了除了需要的所有字段):
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"
}
}]
}]
}]
}]
}