【问题标题】:Find JArray/JObject element within JSON file在 JSON 文件中查找 JArray/JObject 元素
【发布时间】:2021-01-29 17:15:30
【问题描述】:

'products' 标题下,我试图在 'price' 的 JSON 文件中找到 'handle' 变量的第一个值'variants' 标题下的 元素等于 '1.4'

我一直在努力通过 JObject/JArray 进行查询,并想知道是否有办法做到这一点。这是我从中查询的 JSON 文件:

"products":[{
 "id":124534,
 "handle": "Data1", 
 "variants":[
    {
       "barcode":null,
       "sku":"M11-168-EN-NFNE-0",
       "price":1.5
    },
    {
       "barcode":null,
       "sku":"M11-168-EN-NFNE-0",
       "price":1.5
    }]
},
{
 "id":548562,
 "handle": "Data2", 
 "variants":[
    {
       "barcode":null,
       "sku":"M11-168-EN-NFNE-0",
       "price":1.4,
    }]
}]

对此的任何帮助将不胜感激。

【问题讨论】:

  • variants 是一个数组,那么如果其中一个变体的价格是 1.4 而另一个不是呢?该不该选? products[0].variants[0].price = 1.4, products[0].variants[1].price = 1.5, products[1].variants[0].price = 1.4
  • 是的,这就是这里的关键,其他价格不包含它也没关系,只要它被拾取一次就应该得到'handle'值。我确实需要提到 JSON 文件可能会发生变化,这意味着它通常会有更多的变体,而且我每次都会得到不同的价格。使用产品和变体对搜索进行硬编码是行不通的。

标签: c# arrays json linq


【解决方案1】:

假设这些是您的模型类:

public class Product
{
    public int Id { get; set; }
    public string Handle { get; set; }
    public Variant[] Variants { get; set; }   
}

public class Variant
{
    public string Barcode { get; set; }
    public string SKU { get; set; }
    public double Price { get; set; }
}

那么 LINQ 查询将如下所示:

static string GetTheFirstProductWhereOneOfItsVariantHasAGivenPrice(IEnumerable<Product> products, double expectedPrice)
{
    var product = products
        .FirstOrDefault(p => p.Variants.Any(variant => Math.Abs(variant.Price - expectedPrice) < 0.05));
    return product?.Handle ?? "Not Found";
}
  • 它试图找到第一个产品(如果有的话)products.FirstOrDefault
  • 有一个给定价格的变体(在误差范围内)Variants.Any
  • 如果有,则返回其Handle 属性,否则返回Not Found

用法如下所示:

static void Main(string[] args)
{

    const double expectedPrice = 1.4;
    string secondProductWithTheGivenPrice =
        "[{\r\n \"id\":124534,\r\n \"handle\": \"Data1\", \r\n \"variants\":[\r\n    {\r\n       \"barcode\":null,\r\n       \"sku\":\"M11-168-EN-NFNE-0\",\r\n       \"price\":1.5\r\n    },\r\n    {\r\n       \"barcode\":null,\r\n       \"sku\":\"M11-168-EN-NFNE-0\",\r\n       \"price\":1.5\r\n    }]\r\n},\r\n{\r\n \"id\":548562,\r\n \"handle\": \"Data2\", \r\n \"variants\":[\r\n    {\r\n       \"barcode\":null,\r\n       \"sku\":\"M11-168-EN-NFNE-0\",\r\n       \"price\":1.4,\r\n    }]\r\n}]";

    var products = JsonConvert.DeserializeObject<Product[]>(secondProductWithTheGivenPrice);
    Console.WriteLine(GetTheFirstProductWhereOneOfItsVariantHasAGivenPrice(products, expectedPrice));

    string firstProductWithTheGivenPrice =
        "[{\r\n \"id\":124534,\r\n \"handle\": \"Data1\", \r\n \"variants\":[\r\n    {\r\n       \"barcode\":null,\r\n       \"sku\":\"M11-168-EN-NFNE-0\",\r\n       \"price\":1.5\r\n    },\r\n    {\r\n       \"barcode\":null,\r\n       \"sku\":\"M11-168-EN-NFNE-0\",\r\n       \"price\":1.4\r\n    }]\r\n},\r\n{\r\n \"id\":548562,\r\n \"handle\": \"Data2\", \r\n \"variants\":[\r\n    {\r\n       \"barcode\":null,\r\n       \"sku\":\"M11-168-EN-NFNE-0\",\r\n       \"price\":1.4,\r\n    }]\r\n}]";

    products = JsonConvert.DeserializeObject<Product[]>(firstProductWithTheGivenPrice);
    Console.WriteLine(GetTheFirstProductWhereOneOfItsVariantHasAGivenPrice(products, expectedPrice));

    string withoutGivenPrice =
        "[{\r\n \"id\":124534,\r\n \"handle\": \"Data1\", \r\n \"variants\":[\r\n    {\r\n       \"barcode\":null,\r\n       \"sku\":\"M11-168-EN-NFNE-0\",\r\n       \"price\":1.5\r\n    },\r\n    {\r\n       \"barcode\":null,\r\n       \"sku\":\"M11-168-EN-NFNE-0\",\r\n       \"price\":1.6\r\n    }]\r\n},\r\n{\r\n \"id\":548562,\r\n \"handle\": \"Data2\", \r\n \"variants\":[\r\n    {\r\n       \"barcode\":null,\r\n       \"sku\":\"M11-168-EN-NFNE-0\",\r\n       \"price\":1.3,\r\n    }]\r\n}]";

    products = JsonConvert.DeserializeObject<Product[]>(withoutGivenPrice);
    Console.WriteLine(GetTheFirstProductWhereOneOfItsVariantHasAGivenPrice(products, expectedPrice));

}

输出如下所示:

Data2
Data1
Not Found

【讨论】:

  • 感谢您的解释,LINQ 查询是我在这里寻求的,它适用于我的解决方案。祝我的朋友有一个幸福的一天!
猜你喜欢
  • 1970-01-01
  • 2017-01-22
  • 1970-01-01
  • 1970-01-01
  • 2019-06-30
  • 2021-12-18
  • 2017-11-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多