【发布时间】:2019-09-18 11:56:27
【问题描述】:
如何在此列表中找到特定的 id?
var contactList = JsonConvert.DeserializeObject<ContactList>(jsonString);
contactList.contacts.FindAll(x => x.id == item.id);
上面的代码没有按 id 过滤,而是从对象返回所有行。
(Visual Studio 没有向我显示 .Where 子句仅 .Find 和 .FindAll)
C#代码
namespace RestDemo.Model
{
public class Phone
{
public string mobile { get; set; }
public string home { get; set; }
public string office { get; set; }
}
public class Contact
{
public int id { get; set; }
public string name { get; set; }
public string email { get; set; }
public string address { get; set; }
public string gender { get; set; }
public Phone phone { get; set; }
}
public class ContactList
{
public List<Contact> contacts { get; set; }
}
}
json:
{ "contacts": [ { "id": 200, "name": "Ravi Tamada", "email": "ravi@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender": "male", "phone": { "mobile": "+91 0000000000", "home": "00 000000", "office": "00 000000" } }, { "id": 201, "name": "Klev Krist", "email": "klev@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender": "male", "phone": { "mobile": "+91 0000000000", "home": "00 000000", "office": "00 000000" } }, { "id": 202, "name": "Paul Neil", "email": "paul.neil@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender": "male", "phone": { "mobile": "+91 0000000000", "home": "00 000000", "office": "00 000000" } } ]}
谢谢
【问题讨论】:
-
你的 jsonString 长什么样子?
-
告诉我们
ContactList类,contacts和jsonString中有什么 -
您不会在任何地方分配
FindAll的结果。你怎么知道过滤不起作用? -
FindAll 不是“就地”的东西。它返回过滤后的列表。存放在某处
标签: c# json linq object json-deserialization