C# 使用LINQ在JSON中的子对象数组中搜索值
下面是我的JSON:C# 使用LINQ在JSON中的子对象数组中搜索值,c#,arrays,json,linq,C#,Arrays,Json,Linq,下面是我的JSON: [ { "Assignees": [ { "ID": "1111", "IsPrimaryOffice": true }, { "ID": "2222", "IsPrimaryOffice": false } ], "Height": "76", "Width": "78", "Top": "160", "Lef
[
{
"Assignees": [
{
"ID": "1111",
"IsPrimaryOffice": true
},
{
"ID": "2222",
"IsPrimaryOffice": false
}
],
"Height": "76",
"Width": "78",
"Top": "160",
"Left": "99.5"
},
{
"Assignees": [
{
"ID": "3333",
"IsPrimaryOffice": true
},
{
"ID": "4444",
"IsPrimaryOffice": false
}
],
"Height": "11",
"Width": "11",
"Top": "11",
"Left": "11"
},
{
"Assignees": null,
"Height": "22",
"Width": "22",
"Top": "22",
"Left": "22"
},
]
其中,我的数组中的每个主对象都包含一个子对象数组“被分配者”
”
所以我要做的是在数组中搜索每个“被分配者”
”对象,以查找ID
上的匹配项
例如:我想找到受让人
对象,该对象的ID
为“3333”,并且对于IsPrimaryOffice
具有LINQ的true值。我该怎么做?下面是我的想法,但它总是返回null:
mainObject.Where(x =>
x.Assignees != null &&
x.Assignees.Any(y => y.ID == "3333" && y.IsPrimaryOffice == true))
.FirstOrDefault();
有人能帮我吗?提前感谢您我使用提供的json创建了一个类:
var jsonArray = "[{\"Assignees\":[{\"ID\": \"1111\",\"IsPrimaryOffice\": true },
{\"ID\": \"2222\",\"IsPrimaryOffice\": false } ],
\"Height\": \"76\", \"Width\": \"78\", \"Top\": \"160\", \"Left\": \"99.5\" },
{ \"Assignees\": [ {\"ID\": \"3333\",\"IsPrimaryOffice\": true },
{\"ID\": \"4444\",\"IsPrimaryOffice\": false } ],
\"Height\": \"11\", \"Width\": \"11\", \"Top\": \"11\", \"Left\": \"11\" }]";
其生成为:
public class Assignee
{
public string ID { get; set; }
public bool IsPrimaryOffice { get; set; }
}
public class RootObject
{
public List<Assignee> Assignees { get; set; }
public string Height { get; set; }
public string Width { get; set; }
public string Top { get; set; }
public string Left { get; set; }
}
公共类受让人
{
公共字符串ID{get;set;}
公共bool IsPrimaryOffice{get;set;}
}
公共类根对象
{
公共列表被分配者{get;set;}
公共字符串高度{get;set;}
公共字符串宽度{get;set;}
公共字符串Top{get;set;}
左公共字符串{get;set;}
}
现在当我运行下面的查询时
var rootObj = JsonConvert.DeserializeObject<JArray>(jsonArray).ToObject<List<RootObject>>().Where(x =>
x.Assignees != null &&
x.Assignees.Any(y => y.ID == "3333" && y.IsPrimaryOffice == true))
.FirstOrDefault();
foreach (var assignee in rootObj.Assignees)
{
Console.WriteLine("ID = " + assignee.ID);
Console.WriteLine("IsPrimaryOffice = " + assignee.IsPrimaryOffice);
}
var rootObj=JsonConvert.DeserializeObject(jsonArray.ToObject()。其中(x=>
x、 受让人!=null&&
x、 任何(y=>y.ID==“3333”和&y.IsPrimaryOffice==true))
.FirstOrDefault();
foreach(rootObj.受让人中的var受让人)
{
Console.WriteLine(“ID=“+assignee.ID”);
Console.WriteLine(“IsPrimaryOffice=“+受让人.IsPrimaryOffice”);
}
然后将输出作为
ID=3333IsPrimaryOffice=True
ID=4444
IsPrimaryOffice=False
我使用提供的json创建了一个类:
var jsonArray = "[{\"Assignees\":[{\"ID\": \"1111\",\"IsPrimaryOffice\": true },
{\"ID\": \"2222\",\"IsPrimaryOffice\": false } ],
\"Height\": \"76\", \"Width\": \"78\", \"Top\": \"160\", \"Left\": \"99.5\" },
{ \"Assignees\": [ {\"ID\": \"3333\",\"IsPrimaryOffice\": true },
{\"ID\": \"4444\",\"IsPrimaryOffice\": false } ],
\"Height\": \"11\", \"Width\": \"11\", \"Top\": \"11\", \"Left\": \"11\" }]";
其生成为:
public class Assignee
{
public string ID { get; set; }
public bool IsPrimaryOffice { get; set; }
}
public class RootObject
{
public List<Assignee> Assignees { get; set; }
public string Height { get; set; }
public string Width { get; set; }
public string Top { get; set; }
public string Left { get; set; }
}
公共类受让人
{
公共字符串ID{get;set;}
公共bool IsPrimaryOffice{get;set;}
}
公共类根对象
{
公共列表被分配者{get;set;}
公共字符串高度{get;set;}
公共字符串宽度{get;set;}
公共字符串Top{get;set;}
左公共字符串{get;set;}
}
现在当我运行下面的查询时
var rootObj = JsonConvert.DeserializeObject<JArray>(jsonArray).ToObject<List<RootObject>>().Where(x =>
x.Assignees != null &&
x.Assignees.Any(y => y.ID == "3333" && y.IsPrimaryOffice == true))
.FirstOrDefault();
foreach (var assignee in rootObj.Assignees)
{
Console.WriteLine("ID = " + assignee.ID);
Console.WriteLine("IsPrimaryOffice = " + assignee.IsPrimaryOffice);
}
var rootObj=JsonConvert.DeserializeObject(jsonArray.ToObject()。其中(x=>
x、 受让人!=null&&
x、 任何(y=>y.ID==“3333”和&y.IsPrimaryOffice==true))
.FirstOrDefault();
foreach(rootObj.受让人中的var受让人)
{
Console.WriteLine(“ID=“+assignee.ID”);
Console.WriteLine(“IsPrimaryOffice=“+受让人.IsPrimaryOffice”);
}
然后将输出作为
ID=3333IsPrimaryOffice=True
ID=4444
IsPrimaryOffice=False
您是否已将JSON反序列化为适当的对象?您试图找到什么?assignee对象(内部Assignees数组中的项)或外部数组中的项,其中包含Assignees对象及其Assignees数组中的指定参数?您是否将JSON反序列化为正确的对象?您试图查找什么?assignee对象(内部Assignees数组中的项)或外部数组中包含Assignees对象且其Assignees数组中具有指定参数的项?非常感谢!这是完美的。从你的例子中,我现在完全明白了-嵌套LINQ会让我有点困惑。非常感谢!这是完美的。从您的示例中,我现在完全理解了它-嵌套LINQ可能会让我有点困惑。