C#反序列化JSON并找到

时间:2019-09-18 11:56:27

标签: c# json linq object json-deserialization

如何在此列表中找到特定的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"           }       }   ]}

谢谢

4 个答案:

答案 0 :(得分:0)

假设您要查找某个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.
}

答案 1 :(得分:0)

根据您对@Tenretni的回答的评论,我想您错过了在代码中使用System.Linq库的情况。

在您的代码中导入System.Collections.GenericSystem.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

.Net Fiddle

答案 2 :(得分:0)

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
}

答案 3 :(得分:0)

针对您的情况,我使用相同的Model结构反序列化JSON,然后使用Where linq子句来实现您的要求。可以在以下位置找到有效的提琴: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
相关问题