C# 反序列化 json 并查找
C# deserialize json and find
如何在此列表中找到特定的 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" } } ]}
谢谢
假设您要查找某个 id,我们将调用它
var idToFind = "myID";
要查找具有所述 ID 的所有联系人:
var contacts = contactList.contacts.Where(contact=> contact.id == idToFind);
要查找至少一位具有上述 ID 的联系人:
var contactWithID = contactList.contacts.FirstOrDefault(contact=> contact.id == idToFind);
// Before using, check if null, means no contact matched the id.
if(contactWithID != null)
{
// A contact was found.
}
else
{
// No contact matching that id is found.
}
根据你对@Tenretni 的回答的评论,我猜你错过了在你的代码中使用 System.Linq
库。
在您的代码中导入 System.Collections.Generic
和 System.Linq
并使用 FirstOrDefault()
或 .Where()
子句
using System.Collections.Generic;
using System.Linq;
//…
string jsonString = @"{ 'contacts': [{ 'id': 'c200', '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' } }] }";
var contactList = JsonConvert.DeserializeObject<ContactList>(jsonString);
var item = "c200";
var result = contactList.contacts.FirstOrDefault(x => x.id == item);
Console.WriteLine(result.name);
//If you have multiple records with same ID then you can try where clause
var result = contactList.contacts.Where(x => x.id == item); //Here result will be list of Contact
FindAll 方法未将对象分配给搜索结果。
您必须将搜索结果保存在某个地方。
示例多结果
如果您期待多个结果
var contactList = JsonConvert.DeserializeObject<ContactList>(jsonString);
var findedContact = contactList.contacts.FindAll(x => x.id == item.id);
//You business codes..
单个结果示例
如果您只等待 1 个结果
var contactList = JsonConvert.DeserializeObject<ContactList>(jsonString);
var oneContact = contactList.contacts.Find(x => x.id == item.id);
if(oneContact ==null){
//not found business codes
}
else {
//find result business codes
}
针对您的情况,我使用相同的 Model
结构反序列化您的 JSON,然后使用 Where
linq 子句来实现您的要求。可以在以下位置找到有效的 fiddle:https://dotnetfiddle.net/SAcFja
代码:
using System;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
var jsonString = @"{ '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' } }] }";
var data= JsonConvert.DeserializeObject<ContactList>(jsonString);
//Console.WriteLine(data.contacts);
var found=data.contacts.Where(x=>x.id.ToString()=="200");
foreach(var value in found)
{
Console.WriteLine(value.id);
Console.WriteLine(value.address);
}
}
}
public class Phone
{
public string mobile { get; set; }
public string home { get; set; }
public string office { get; set; }
}
public class Contact
{
public string 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; }
}
id=200时输出:
200
xx-xx-xxxx,x - street, x - country
如何在此列表中找到特定的 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" } } ]}
谢谢
假设您要查找某个 id,我们将调用它
var idToFind = "myID";
要查找具有所述 ID 的所有联系人:
var contacts = contactList.contacts.Where(contact=> contact.id == idToFind);
要查找至少一位具有上述 ID 的联系人:
var contactWithID = contactList.contacts.FirstOrDefault(contact=> contact.id == idToFind);
// Before using, check if null, means no contact matched the id.
if(contactWithID != null)
{
// A contact was found.
}
else
{
// No contact matching that id is found.
}
根据你对@Tenretni 的回答的评论,我猜你错过了在你的代码中使用 System.Linq
库。
在您的代码中导入 System.Collections.Generic
和 System.Linq
并使用 FirstOrDefault()
或 .Where()
子句
using System.Collections.Generic;
using System.Linq;
//…
string jsonString = @"{ 'contacts': [{ 'id': 'c200', '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' } }] }";
var contactList = JsonConvert.DeserializeObject<ContactList>(jsonString);
var item = "c200";
var result = contactList.contacts.FirstOrDefault(x => x.id == item);
Console.WriteLine(result.name);
//If you have multiple records with same ID then you can try where clause
var result = contactList.contacts.Where(x => x.id == item); //Here result will be list of Contact
FindAll 方法未将对象分配给搜索结果。
您必须将搜索结果保存在某个地方。
示例多结果
如果您期待多个结果
var contactList = JsonConvert.DeserializeObject<ContactList>(jsonString);
var findedContact = contactList.contacts.FindAll(x => x.id == item.id);
//You business codes..
单个结果示例
如果您只等待 1 个结果
var contactList = JsonConvert.DeserializeObject<ContactList>(jsonString);
var oneContact = contactList.contacts.Find(x => x.id == item.id);
if(oneContact ==null){
//not found business codes
}
else {
//find result business codes
}
针对您的情况,我使用相同的 Model
结构反序列化您的 JSON,然后使用 Where
linq 子句来实现您的要求。可以在以下位置找到有效的 fiddle:https://dotnetfiddle.net/SAcFja
代码:
using System;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
var jsonString = @"{ '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' } }] }";
var data= JsonConvert.DeserializeObject<ContactList>(jsonString);
//Console.WriteLine(data.contacts);
var found=data.contacts.Where(x=>x.id.ToString()=="200");
foreach(var value in found)
{
Console.WriteLine(value.id);
Console.WriteLine(value.address);
}
}
}
public class Phone
{
public string mobile { get; set; }
public string home { get; set; }
public string office { get; set; }
}
public class Contact
{
public string 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; }
}
id=200时输出:
200
xx-xx-xxxx,x - street, x - country