[
{
"id": "133",
"label": "S/M",
"price": "0",
"oldPrice": "0",
"products": [
"318",
"321",
"324",
"327"
]
},
{
"id": "132",
"label": "L/XL",
"price": "0",
"oldPrice": "0",
"products": [
"319",
"322",
"325",
"328"
]
},
{
"id": "131",
"label": "XXL/XXXL",
"price": "0",
"oldPrice": "0",
"products": [
"320",
"323",
"326",
"329"
]
}
]
我希望得到'标签'阵列"产品"包含" 321"。我怎么能做到这一点?我用了库json.net
i make linq expression
JArray ja = JArray("this json");
JValue id = JValue.Parse("328");
ja.Select(x => x["label"]).Where(x => x["products"].Contains(id));
但我得到"无法访问Newtonsoft.Json.Linq.JValue上的子值。"
答案 0 :(得分:1)
所以你应该先定义这个类:
class MyObj {
public string id { get; set; }
public string[] products { get; set; }
public string label { get; set; }
}
反序列化而不是对象:
var deserialized = serializer.Deserialize<MyObj>(str);
var result = deserialized.Where(r => r.products.Contains("321")).ToList();
答案 1 :(得分:1)
您需要使用JSON.NET之类的库。 在这种情况下,你可以写这样的东西
string json = <your json string>;
var deserializedProduct = JsonConvert.DeserializeObject<List<Product>>(json).Where(p => p.products.Contains("321")).ToList();
产品
public class Product
{
public string id { get; set; }
public string[] products { get; set; }
public string label { get; set; }
}
答案 2 :(得分:1)
为此,您可以使用任何JSON库。例如JSON.NET
此LINQ to JSON样本