-2

How to convert JSON output value in Array List C#.

Input

[  
   {  
      "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
   }
]

What i have so far

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/>");
}
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
AJIT AGARWAL
  • 11
  • 1
  • 6

1 Answers1

1

You can use the LINQ Distinct method to remove the duplicates.

Start by defining an IEqualityComparer<Employee> that tells us whether two employees are equal:

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;
    }
}

Now that we defined how to compare two employees, we use this to remove the duplicates by applying Distinct to the unfiltered collection:

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

List<Employee> distinctEmployees = objListEmpl.Distinct(new EmployeeComparer()).ToList();
Georg Patscheider
  • 9,357
  • 1
  • 26
  • 36