C# 如何用C语言将json对象转换成列表,避免重复记录#

C# 如何用C语言将json对象转换成列表,避免重复记录#,c#,json,C#,Json,如何在数组列表C#中转换JSON输出值 输入 [ { "Name":"Biplab", "Address":"Kolkata", "Gender":"Male", "Salary":110000 }, { "Name":"Biplab", "Address":"Kolkata", "Gender":"Male", "Salary":110000 }, {

如何在数组列表C#中转换JSON输出值

输入

[  
   {  
      "Name":"Biplab",
      "Address":"Kolkata",
      "Gender":"Male",
      "Salary":110000
   },
   {  
      "Name":"Biplab",
      "Address":"Kolkata",
      "Gender":"Male",
      "Salary":110000
   },
   {  
      "Name":"Om",
      "Address":"Kolkata",
      "Gender":"Male",
      "Salary":25000
   },
   {  
      "Name":"Pankaj",
      "Address":"Kolkata",
      "Gender":"Male",
      "Salary":20000
   }
]
到目前为止我拥有的

string jsonvalue = "[{\"Name\":\"Biplab\",\"Address\":\"Kolkata\",\"Gender\":\"Male\",\"Salary\":110000},{\"Name\":\"Biplab\",\"Address\":\"Kolkata\",\"Gender\":\"Male\",\"Salary\":110000},{\"Name\":\"Om\",\"Address\":\"Kolkata\",\"Gender\":\"Male\",\"Salary\":25000},{\"Name\":\"Pankaj\",\"Address\":\"Kolkata\",\"Gender\":\"Male\",\"Salary\":20000}]";

List <Employee>objListEmpl= ( List <Employee >) javaScript.Deserialize(jsonvalue,typeof (List <Employee >));

foreach (Employee objEmptest in objListEmpl)
{
  Response.Write("Name = "+ objEmptest.Name +"<br/>");
  Response.Write("Address = " + objEmptest.Address  + "<br/>");
  Response.Write("Gender = " + objEmptest.Gender + "<br/>");
  Response.Write("Salary = " + objEmptest.Salary + "<br/>");
}
10.代码>字符串json<<代码>字符串json10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0,,,,,,,,,,,,,,,,,,,,,,,,“性别”:“男性”,“工资”\":20000}]"; List objListEmpl=(List)javaScript.Deserialize(jsonvalue,typeof(List)); foreach(对象列表中的员工对象测试) { 响应。写入(“Name=“+objEmptest.Name+”
); 响应。写入(“地址=“+objEmptest.Address+”
); 响应。写入(“性别=“+ObjemTest.Gender+”
); 响应。写入(“Salary=“+objEmptest.Salary+”
); } 您可以使用LINQ方法删除重复项

首先定义一个
IEqualityComparer
,告诉我们两名员工是否相等:

public class EmployeeComparer : : IEqualityComparer<Employee> {
    // Employees are equal if their porperties are equal.
    public bool Equals(Employee x, Employee y) {

        //Check whether the compared objects reference the same data.
        if (Object.ReferenceEquals(x, y)) return true;

        // Check whether any of the compared objects is null.
        if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
            return false;

        // Check whether the employee's properties are equal.
        return x.Name == y.Name 
            && x.Address == y.Address
            && x.Gender == y.Gender
            && x.Salary == y.Salary;
    }

    // If Equals() returns true for a pair of objects 
    // then GetHashCode() must return the same value for these objects.
    public int GetHashCode(Employee employee){
        // Check whether the object is null
        if (Object.ReferenceEquals(employee, null)) return 0;

        // Get hash codes for properties if they are not null.
        int hashEmployeeName = employee.Name == null ? 0 : employee.Name.GetHashCode();
        int hashEmployeeAddress = employee.Address == null ? 0 : employee.Address.GetHashCode();
        int hashEmployeeGender = employee.Gender == null ? 0 : employee.Gender.GetHashCode();
        int hashEmployeeSalary = employee.Salary == null ? 0 : employee.Salary.GetHashCode();

        // Calculate the hash code for the whole object using XOR.
        return hashEmployeeName ^ hashEmployeeAddress ^ hashEmployeeGender ^ hashEmployeeSalary;
    }
}

这不是有效的JSON。只允许一个顶级对象。使用数组。[{“名称”:“Biplab”,“地址”:“加尔各答”,“性别”:“男性”,“薪水”:110000},{“名称”:“Biplab”,“地址”:“加尔各答”,“性别”:“男性”,“薪水”:110000},{“名称”:“Om”,“地址”:“加尔各答”,“性别”:“男性”,“薪水”:25000},{“名称”:“Pankaj”,“地址”:“加尔各答”,“性别”:“男性”,“薪水”:“25000“:20000}]阅读并解释此代码的作用或不作用。JSON文件ok。凯克在线
List<Employee> objListEmpl = (List<Employee>) javaScript.Deserialize(jsonvalue,typeof(List Employee>));

List<Employee> distinctEmployees = objListEmpl.Distinct(new EmployeeComparer()).ToList();