我有一组包含联系方式的 JSON 对象,当它为真时,我必须根据该字段过滤它们。
这是示例数据
{
"9002":{
"Contacts": [
{
"Source": 0,
"Id": 0,
"Details": {
"Harlson": "9015",
"adssd": "9022",
"First Name": "Gary",
"Last Name": "Harlson"
},
"Pinned": true
}
]
}
}
我想使用 LINQ 查询根据 Pinned
变为 true
时过滤所有详细信息。
您可以反序列化 Json(使用 NewtonSoft Json)并使用 Linq 通过 Pinned=True
查询项目
var rootInstance = JsonConvert.DeserializeObject<RootObject>(json);
var result = rootInstance.Id.Contacts.Where(x=>x.Pinned);
类定义为
public class Details
{
[JsonProperty("Harlson")]
public string Harlson { get; set; }
[JsonProperty("adssd")]
public string adssd { get; set; }
[JsonProperty("First Name")]
public string FirstName { get; set; }
[JsonProperty("Last Name")]
public string LastName { get; set; }
}
public class Contact
{
public int Source { get; set; }
public int Id { get; set; }
public Details Details { get; set; }
public bool Pinned { get; set; }
}
public class Id
{
public List<Contact> Contacts { get; set; }
}
public class RootObject
{
[JsonProperty("9002")]
public Id Id { get; set; }
}